Page 390 - Introduction to Programming with Java: A Problem Solving Approach
P. 390
356 Chapter 9 Classes with Class Members
PennyJar
+GOAL : int = 10000
-allPennies : int = 0 -pennies : int = 0
+addPenny() : void +getPennies() : int +getAllPennies() : int
Figure 9.4 Class describing penny jars individually and as a group
to $100.00. Presumably, when the user reaches the GOAL amount, he/she will empty all penny jars and
spend all the money in a big shopping spree.
• The allPennies variable stores the total pennies in all jars. Since allPennies is an attribute of
all the penny jars, it’s a class member and uses the static modifier. Although we could just accept the
zero default as the initial value, we explicitly initialize to zero to emphasize what we want.
• The pennies variable is an ordinary instance variable. Again, although we could just accept the zero
default as the initial value, we explicitly initialize to zero to emphasize what we want.
Let’s now examine the method definitions:
• The getPennies methodAispa taypgicaol accPessDorFmethEodn, ahndaitnretcrieevesrthe value of the pennies instance variable. Accessing the pennies instance variable means that getPennies must be an instance method. You can see that getPennies is an instance method because there’s no static modifier in its heading.
• The addPenny method simulates adding a penny to a jar. It updates the pennies instance variable for the jar that the penny was added to and updates the allPennies class variable for the collection of jars. Accessing the pennies instance variable means that addPenny must be an instance method, and, as such, there’s no static modifier in its heading.
• The getAllPennies method retrieves the value of the allPennies class variable. Since getAllPennies deals only with class-wide data, it’s appropriate to make it a class method. You can see that getAllPennies is a class method because of the static modifier in its heading.
Driver
Figure 9.6 contains a driver for the PennyJar class. Notice how main calls the instance methods addPenny and getPennies by first creating PennyJar objects. It assigns the newly created objects to the reference variables pennyJar1 and pennyJar2. Then it uses those reference variables to call the instance methods.
Notice how main uses a different technique for calling getAllPennies. Instead of prefixing getAllPennies with a reference variable (pennyJar1 or pennyJar2), main calls getAllPennies by prefixing it with the PennyJar class name. That’s because getAllPennies is a class method, not an instance method. Would it be OK to omit the class name prefix and just call getAllPennies directly? No. We cannot omit the class name prefix because getAllPennies is in a separate class. If we had merged main into the PennyJar class to run the program from that class, then we could omit the PennyJar prefix from the getAllPennies method call. However, it never hurts to include the class name prefix for a class method call. It makes it easier to cut and paste, and it helps make the code more self-documenting.