amount
variable its first value:double amount;
amount = 5.95;
You can do the same thing with just one line:
double amount = 5.95;
When you do this, you don’t say that you’re “assigning” a value to the amount variable. The line
isn’t called an “assignment statement.” Instead, this line is called a declaration with an initialization. You’re initializing the double
amount=5.95amount
variable. You can do all sorts of things with initializations, even arithmetic:
doublegasBill = 174.59;
doubleelecBill = 84.21;
double H2OBill = 22.88;
double total = gasBill + elecBill + H2OBill;
Moving Java variables from place to place
It helps to remember the difference between initializations and assignments. For one thing, you can drag a declaration with its initialization outside of a method://This is okay:
classSnitSoft {
static double amount = 5.95;
public static void main(String args[]) {
amount = amount + 25.00;
System.out.print("We will bill $");
System.out.print(amount);
System.out.println(" to your credit card.");
}
}
You can’t do the same thing with assignment statements:
//This does not compile:
classBadSnitSoftCode {
static double amount;
amount = 5.95; //Misplaced statement
public static void main(String args[]) {
amount = amount + 25.00;
System.out.print("We will bill $");
System.out.print(amount);
System.out.println(" to your credit card.");
}
}
You can’t drag statements outside of methods. (Even though a variable declaration ends with a semicolon, a variable declaration isn’t considered to be a statement. Go figure!)
Notice how the word static
was added to each declaration that was pulled out of the main
method. This was done because the main
method’s header has the word static
in it. Not all methods are static. In fact, most methods aren’t static. But whenever you pull a declaration out of a static
method, you have to add the word static
at the beginning of the declaration.
Combining variable declarations in Java
This code has only one variable (as if variables are in short supply). You can get the same effect with several variables:classSnitSoftNew {
public static void main(String args[]) {
doubleflashDrivePrice;
doubleshippingAndHandling;
double total;
flashDrivePrice = 5.95;
shippingAndHandling = 25.00;
total = flashDrivePrice + shippingAndHandling;
System.out.print("We will bill $");
System.out.print(total);
System.out.println(" to your credit card.");
}
}
The new code has three declarations — one for each of the program’s three variables. Because all three variables have the same type (the type double
), you can modify the code and declare all three variables in one fell swoop:
doubleflashDrivePrice, shippingAndHandling, total;
Which is better — one declaration or three declarations? Neither is better. It’s a matter of personal style.
You can even add initializations to a combined declaration. When you do, each initialization applies to only one variable. For example, with the line
doubleflashDrivePrice, shippingAndHandling = 25.00, total;
the value of shippingAndHandling
becomes 25.00
, but the variables flashDrivePrice
and total
get no particular value.