Page 384 - Introduction to Programming with Java: A Problem Solving Approach
P. 384
350 Chapter 9 Classes with Class Members
To declare a class method, use this syntax for the method heading: <private-or-public> static <return-type> <method-name>(<parameters>)
Note how Figure 9.2’s printMouseCount method follows that syntax pattern.
Normally, to access a class member, you should prefix the class member with the class member’s
class name and then a dot. For example, within the printMouseCount method and the Mouse4 con- structor, note how mouseCount and youngestMouse are accessed with Mouse4 dot prefixes— Mouse4.mouseCount and Mouse4.youngestMouse. Also, within the main method, see how the printMouseCount class method is called with Mouse4.printMouseCount(). Prefixing a class member with its class name and then a dot should look familiar. You’ve done that with Math class members for quite a while (e.g., Math.round(), Math.PI).
Be aware that you don’t always have to use the class name dot prefix when accessing a class member. In accessing a class member, you may omit the class name dot prefix if the class member is in the same class as the class from which you’re trying to access it. So in the Mouse4 class, since all the class member accesses and the class members themselves are in the same Mouse4 class, all the class name dot prefixes can be omitted. But if the program were written with main appearing in a separate driver class, then the Mouse4 dot could not be omitted from main’s call to printMouseCount.
Although it’s often legal to omit the class name dot prefix, we have a slight preference for always in- cluding it because it’s a form of self documentation. It alerts the person reading the code to the fact that the accessed member is special—it deals with class-wide information.
Calling an Instance Method from within a Class Method
Apago PDF Enhancer
If you’re within a class method, you’ll get a compilation error if you attempt to access an instance member directly. To access an instance member, you first must have an object, and then you access the object’s in- stance member by prefacing it with the object’s reference variable. The reference variable is often referred to as the calling object. Does all that sound familiar? The main method is a class method (main’s heading in- cludes the static modifier), and you’ve been calling instance methods from main for quite a while now. But whenever you do that, you first must instantiate an object and assign the object’s reference to a reference variable. Then you call the instance method by prefixing it with the reference variable and a dot. Figure 9.2’s main method shows what we’re talking about:
public static void main(String[] args)
{
}
Mouse4 pinky = new Mouse4();
pinky.olderByOneDay();
Mouse4.printMouseCount();
Reference variable dot prefix is necessary when calling an instance method from within a class method
If you attempt to access an instance method directly from within a class method, you’ll see an error message like this:
Non-static<method-name> cannot be referenced from a static context
That error message is very common (you’ve probably seen it many times) because it’s easy to forget to prefix instance method calls with a reference variable. When veteran programmers see it, they know what to do; they make sure to prefix the instance method call with a calling object’s reference variable. But when beginning programmers see the error message, they often compound the error by trying to “fix” the bug inappropriately.