Page 391 - Introduction to Programming with Java: A Problem Solving Approach
P. 391
9.6 Using Class Members in Conjunction with Instance Members 357
/********************************************************************
*
PennyJar.java
Dean & Dean
This class counts pennies for individual penny jars and for
all penny jars combined.
********************************************************************/
*
*
*
*
public class PennyJar
{
⎫ public static final int GOAL = 10000; ⎬
class variables
private static int allPennies = 0
private int pennies = 0;
⎭
}
⎪ // end class PennyJar ⎭
//*****************************************************************
public int getPennies()
{
}
return this.pennies;
//*****************************************************************
public void adAdpPeanngy(o) PDF Enhanc⎫er {⎪
⎪ System.out.println("Clink!"); ⎪
this.pennies++; ⎪ PennyJar.allPennies++; ⎪
⎬ if (PennyJar.allPennies >= PennyJar.GOAL)⎪
{⎪
⎪
⎪ }⎪ // end addPenny ⎭
instance method
}
System.out.println("Time to spend!");
//*****************************************************************
public static int getAllPennies() ⎫ ⎪
{⎪ return PennyJar.allPennies; ⎬
class method
}⎪
;
instance variable
Figure 9.5 A PennyJar class that illustrates both instance members and class members