Page 242 - Introduction to Programming with Java: A Problem Solving Approach
P. 242

                208 Chapter 6 Object-Oriented Programming
The gus object is the calling object for these statements:
gus.setPercentGrowthRate(growthRate);
gus.grow();
gus.display();
Are there any other calling objects? Yes. The stdIn object is a calling object in this statement: growthRate = stdIn.nextDouble();
The this Reference
It’s easy to identify the calling object when you’re looking at a method call statement. But what if you’re in- side the called method—how can you tell what object called the method? For example, when you’re looking at the definition of the Mouse class in Figure 6.4, can you identify the calling object that called its grow method? Here is that method again:
public void grow()
{
}
this.weight +=
(0.01 * this.percentGrowthRate * this.weight);
this.age++;
// end grow
The pronoun this (called the this reference) stands for the calling object, but it doesn’t tell you which object that is. Thus, you cAanp’t taellgwohat thPe cDalFling oEbjencthisajusnt bcyeloorking at the method that was called. You must look at what called that method. If the statement that called grow was gus.grow(), then gus is the calling object. Alternately, if the statement that called grow was jaq.grow(), then jaq is the calling object. As you’ll see when we do the upcoming trace, you must know which object, gus or jaq, is the current calling object so that you update the proper object. Within the above grow method, note this. weight and this.age. The this reference reminds you that weight and age are instance variables. Instance variables in which object? In the calling object!
The setPercentGrowthRate method in Figure 6.4 provides another example. Here is that method again:
public void setPercentGrowthRate(double percentGrowthRate)
{
this.percentGrowthRate = percentGrowthRate;
} // end setPercentGrowthRate
The this reference tells you the variable on the left side of this method’s lone statement is an instance vari- able in the calling object. As indicated earlier, the this reference in this statement also helps the compiler and a human distinguish the variable on the left side from the variable on the right side. Before the advent of OOP, computer languages did not include this dot functionality. Then, the only way the compiler and a human could distinguish between variables in different places that referred to essentially the same thing was to give them similar but slightly different names.
The ad hoc (special case) nature of how old-time programmers devised slightly different names made programs confusing and increased programming errors. Java’s this reference provides a standard way to make the distinction and show the relationship at the same time. You can use exactly the same name to show the relationship and then use this dot to make the distinction. So it is no longer necessary to use slightly different names for that purpose, and we recommend against that archaic practice.












































































   240   241   242   243   244