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

                8.11 Accessing Instance Variables Without Using this 327
Up until now, we’ve split each of our OOP programs into separate classes—a driver class and one or more driven classes. It’s easiest to grasp the concept of an object if it’s associated with one class, while the code that instantiates it is associated with another class. Driven classes and driver classes have distinc- tive roles. A driven class describes a thing that’s being modeled. For example, in our Mouse programs, the Mouse class describes a mouse. A driver class contains a main method, and it drives the separate Mouse class. In our Mouse programs, the MouseDriver class instantiates Mouse objects and performs actions on those objects. Using two or more classes fosters the habit of putting different types of things in different modules.
Although we’ll continue to use separate classes for most of our programs, for short programs that don’t do much except demonstrate a concept, we’ll sometimes merge main into the class that implements the rest of the program. It’s a matter of convenience—there’s one less file to create and there’s slightly less code to enter.
      In a big program that has one driver class in charge of a large number of driven classes,
it’s sometimes handy to insert an additional main method in some or all of the driven
classes. The additional main method in a driven class serves as a local tester for the code
in that class. Whenever you make a change in the code of a particular class, you can use its
local main method to test that class directly. It’s easy. Just execute the class of interest, and the JVM au- tomatically uses that class’s main method. Once you’ve verified the changes you’ve made locally, you can proceed to execute the driver in a higher-level module to test more or all of the program. You don’t have to remove the local main methods. You can just leave them there for future local testing or demonstration of the features of each particular class. When you execute the overall program’s driver class, the JVM auto- matically uses the main method in that driver class, and it ignores any main methods that may happen to be in other classes in the pArogpraam.go PDF Enhancer
Thus, you can add a main method to any class, so that the class can be executed directly and act as its own driver. When a multiclass program contains multiple main methods (no more than one per class), the particular main method that’s used is the one in the class that’s current when execution starts.
8.11 Accessing Instance Variables Without Using this
For a while now, we’ve used this to access the calling object’s instance variables from within a method.
Here’s a formal explanation for when to use this:
Use this within an instance method or a constructor to access the calling object’s instance variables. The this reference distinguishes instance variables from other variables (like local variables and parameters) that happen to have the same name.
However, if there is no name ambiguity, you may omit the this prefix when accessing an instance variable.
The code in Figure 8.11 has several places where the this prefix is worth mentioning. It’s OK to omit this in the statement in the setAge method, because the instance variable name is different from the parameter name. It’s not OK to omit this in the statement in the setWeight method, because the simi- larity in instance variable and parameter names would create an ambiguity. It is OK to omit this in the statement in the print method, because there is no name ambiguity.
Sometimes an instance method is called by one object and has a parameter which refers to a different object in the same class. String’s equals method is a familiar example of this situation. Inside such a method, there will be code that needs to refer to two different objects, the calling object and the object
Provide each class with a built-in test method.
       

















































































   359   360   361   362   363