Page 243 - Introduction to Programming with Java: A Problem Solving Approach
P. 243
To emphasize the meaning and utility of Java’s this reference, we will use it with all examples of instance variables up to the end of the next chapter—even when it is not necessary to draw a distinction be- tween an instance variable and a parameter. There is no performance penalty in using this dot, and it pro- vides an immediate indicator to everyone that the variable is an instance variable. Thus, it helps to explain the program; that is, it provides useful self documentation.
6.6 Instance Variables
You’ve been exposed to instance variables for a while now. You know that an object stores its data in in- stance variables. You know that an instance method accesses its instance variables by prefacing them with the this reference (e.g., this.weight). In this section, we consider a few more instance variable details. Specifically, we consider default values and persistence.
Default Values for Instance Variables
As implied by the common definition of “default,” a variable’s default value is the variable’s value when there’s no explicitly assigned initial value; that is, when there’s no initialization. Different types of variables have different default values.
There are two integer types that we’ve covered so far—int and long. Integer-type instance variables are assigned 0 by default. But in the Mouse class, notice that we initialize the age instance variable to 0:
Apago PDF Enhancer
Why bother with the explicit initialization? Wouldn’t age be assigned 0 by default even if “= 0” was omit- ted? Yes, the program would work the same either way. But it’s poor practice to depend on hidden default val- ues. By explicitly assigning values to variables, we show our intent. That’s a form of self-documenting code.
There are two floating-point types—float and double. Floating-point-type instance variables are assigned 0.0 by default. The Mouse class declares two floating-point instance variables—weight and percentGrowthRate:
private int age = 0; // age of mouse in days
private double weight = 1.0;
// weight of mouse in grams
private double percentGrowthRate; // % weight increase per day
In this case, we initialize the weight instance variable to 1.0, so the default value doesn’t come into play. We do not initialize the percentGrowthRate value, so percentGrowthRate is initialized to 0.0 by de- fault. Didn’t we just say that it’s poor practice to depend on hidden default values? Yes, but in this case, we’re not depending on the default value. In the MouseDriver class, we overlay the percentGrowthRate default value with a custom value by calling setPercentGrowthRate like this:
gus.setPercentGrowthRate(growthRate);
boolean instance variables are assigned false by default. For example, if you added a boolean instance variable named vaccinated to the Mouse class, vaccinated would be assigned false by default.
Reference-type instance variables are assigned null by default. For example, if you added a String instance variable named breed to the Mouse class, breed would be assigned null by default. Normally, a reference variable holds the address of an object and that address points to an object. The Java designers added null to the language as a way to indicate that a reference variable points to nothing. So the default for a reference-type instance variable is to point to nothing.
6.6 InstanceVariables 209