Page 252 - Introduction to Programming with Java: A Problem Solving Approach
P. 252
218 Chapter 6 Object-Oriented Programming
Let’s examine Figure 6.13’s Mouse2Driver class. In the call to setPercentGrowthRate, note that we pass in a constant, 10, instead of a variable. Normally, you’ll use variables for your arguments, but this example shows that it’s legal to use constants also. After setting the percent growth rate, we prompt the user for the number of days of simulated growth, and then we pass the days value into the grow method. Then we print mickey’s age and weight by embedding getAge and getWeight method calls within a printf statement.
Mouse2 Class
Now look at the Mouse2 class in Figure 6.14. Are there any local variables there? The age, weight, and percentGrowthRate variables are instance variables, not local variables, because they’re declared out- side of all the methods, at the top of the class. Inside the grow method, we highlight this fact by prefixing each of these instance variables with a this reference. The grow method also includes a local variable— the i in the for loop. Since i is declared within the for loop header, its scope is limited to the for loop block. So you can read and update i only within the for loop. If you try to access i outside the for loop, you’ll get a compilation error. This grow method is similar to the previous Mouse program’s grow method, but this time we use a for loop to simulate multiple days of growth rather than just one day. The days pa- rameter determines how many times the loop will repeat.
Previously we described the default values for instance variables. Now we’ll describe the default values
for local variables. Local variables contain garbage by default. Garbage means that the variable’s value is
unknown—it’s whatever just happens to be in memory at the time that the variable is created. If a program
attempts to access a variable that contains garbage, the compiler generates a compilation error. For example,
what would happen if the =0 initialization were removed from the for loop header in the grow method in Apago PDF Enhancer
Figure 6.14? In other words, suppose that for loop was replaced by this: for (int i; i
{
}
this.weight +=
(0.01 * this.percentGrowthRate * this.weight);
Since i is no longer assigned zero, i contains garbage when the i<days condition is tested. If you tried to compile code with a statement like this, it wouldn’t compile, and the compiler would report:
variable i might not have been initialized
Local Variable Persistence
OK, let’s say you do initialize a local variable. How long will it persist? A local variable (or parameter) persists only within its scope and only for the current duration of the method (or for loop) in which it is defined. The next time the method (or for loop) is called, the local variable’s value resets to the value given it by whatever initialization it gets. The horizontal line drawn in a trace after a method terminates reminds you that method termination converts all the method’s local variables into garbage.
6.10 The return Statement
If you look back at our original Mouse class in Figures 6.4 and 6.10, you’ll notice that every method head- ing has a void modifier located at the left of the method name. That means the method does not return any value, and we say “the method has a void return type” or more simply “it’s a void method.” But recall from