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

                More specifically, when confronted with the non-static method error message, a beginning programmer will often change the offending instance method to a class method, by inserting static in the method’s head- ing. (In the Mouse4 program, olderByOneDay would be changed to a class method). They then get the non-static member error message for any instance variables within the method. They then compound the problem further by changing the method’s instance variables to class variables. (In the Mouse4 program, olderByOneDay’s age variable would be changed to a class variable). With that change in place, the pro- gram compiles successfully and the beginning programmer is happy as a lark, ready to slay the next dragon. Unfortunately, that type of solution leads to a worse problem than a compilation error. It leads to a logic error.
As you know, if a class’s member relates to one object rather than to the class as a whole, you should make it an instance member. If you do as described above and “fix” a bug by changing an instance member to a class member, you can get your program to compile and run. And if you have only one object, your program might even produce a valid result. But if you have more than one object, either now or in the future, then with class variables, the objects will share the same data. If you change one object’s data, you’ll simul- taneously change all other objects’ data, and normally that would be incorrect.
Aside: Accessing a Class Member from an Instance Method or Constructor
Although you can’t access an instance member directly from a class method, you can access a class member from an instance method. In addition, you can access a class variable from a constructor, and Figure 9.2 il- lustrates that. The relevant code is repeated below for your convenience. It shows how the mouseCount and youngestMouse class variables are updated automatically with each new instantiation. Note how the this reference assigns the constructor’s newly instantiated mouse to the youngestMouse variable.
public Mouse4() Apago PDF Enhancer {
}
Mouse4.mouseCount++;
Mouse4.youngestMouse = this;
When To Use Class Methods
When should you make a method a class method? The general answer is “when you need to perform a task that involves the class as a whole.” But let’s get more specific. Here are situations where class methods are appropriate:
1. If you have a method that uses class variables and/or calls class methods, then it’s a good candidate for being a class method. For example, Figure 9.2’s printMouseCount is a class method because it prints the mouseCount class variable. Warning: If in addition to accessing class members, the method also accesses instance members, then the method must be an instance method, not a class method.
2. If you might need to call a method even when there are no objects from the method’s class, then you should make it a class method. For example, during a mouse population simulation, you might call printMouseCount when there are no mouse objects (they’ve all died perhaps). Since it’s a class method, you do it like this, without needing a calling object:
Mouse4.printMouseCount();
3. Themainmethodisthestartingpointforallprogramsand,assuch,itgetsexecutedpriortotheinstan- tiation of any objects. To accommodate that functionality, you’re required to make the main method a class method. If your main method is rather long and you decide to break it up with helper methods,
9.3 Class Methods 351




















































































   383   384   385   386   387