Chaster is correct, you must define your variables before you use them. A variable in computer science is the same as it is in normal algebra. For example, x and y are commonly used variables in algebra. Similarly, in Java programming, you can define your own variables. To define a variable of type double named Age of Interest, do the following.
double age_Of_Interest;
Please note the following things about the declaration above:
- it is intentionally spelled age_Of_Interest because, by convention, variable names start with a lowercase letter. Each word in the variable name (except the first word) starts with an uppercase letter.
- Java does not allow you to put spaces in your variable names, so if you want to separate words, you have to do so using underscores. ( _ )
- After the declaration above, age_Of_Interest does not have a value. Giving a variable a value is called "initializing the variable". So after we declared the variable, if we had said age_Of_Interest = 3.5, that is initializing the variable.
- A variable can only be declared once. When you first put the type and the name of the variable, that declares it. So double age_Of_Interest; is a variable declaration.
- You should not use a variable before it is declared and initialized. And the compiler will not let you use a variable that is not declared, which is why you got errors.
- The first time you say double age_Of_Interest; you are creating a variable called age_Of_Interest. After you do that, if you say double age_Of_Interest; again, you are trying to create a variable that already exists, which does not make sense and is not allowed. To use the variable after its been declared/created, all you have to do is say age_Of_Interest.
Last edited by BestJewSinceJC; Nov 1st, 2008 at 4:54 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
Offline 2,758 posts
since Sep 2008