Page 381 - Introduction to Programming with Java: A Problem Solving Approach
P. 381
Class Variable Examples
As you know, each use of new creates a separate copy of all instance variables for each object. Class vari- ables are different. For a particular class, there is only one copy of each class variable, and all objects share that single copy. Thus, you should use class variables to describe properties of a class’s objects that need to be shared by all of the objects. For example, consider again the problem of simulating mouse growth. In our previous mouse programs, we kept track of data pertinent to each individual mouse—a mouse’s growth rate, a mouse’s age, and a mouse’s weight. For a more useful simulation program, you’d probably also want to keep track of group data and common environmental data. For example:
mouseCount would keep track of the total number of mice.
youngestMouse would keep track of which mouse was born last.
averageLifeSpan would keep track of the average life span for all of the mice. simulationDuration would limit the number of simulation iterations.
researcher would identify a person in charge of an experiment on the group of mice. noiseOn would indicate the presence or absence of a stressful noise heard by all the mice.
If you used instance variables for mouseCount, averageLifeSpan, and so on, each individual mouse object would have its own copy of that data. So if there were one hundred total mice, each of the one hun- dred mice would store the value 100 in its own mouseCount variable, the average life span value in its own averageLifeSpan variable, and so on. This would mean that every time a new mouse was born or died or aged a year, you would have to update 100 separate copies of mouseCount, averageLifeSpan, and so on—all with exactly the same information. What a waste of effort! Why not just do it once and let every-
one write and read the same common data? If mouseCount, averageLifeSpan, and so on are class Apago PDF Enhancer
variables, all mouse objects can write to and read from a single record of each of these pieces of information. An outsider can access these class properties by just prefixing the class name to an appropriate class method. It’s neither necessary nor desirable to go through a particular instance to get to this group information.
The class variable declarations in our enhanced Mouse class would look something like the code in Figure 9.1. In the figure, does it strike you as odd that the type of youngestMouse is the name of the class in which it is defined? Does that mean there’s a mouse within a mouse? No! The static modifier in youngestMouse’s declaration means that youngestMouse is a class variable. As such, it’s a property of the collection of all mice. More specifically, it identifies the mouse object that was most recently instanti- ated. In the next section, we present youngestMouse in the context of a complete program, and you’ll see how it gets updated every time there’s a mouse object instantiation.
9.2 Class Variables 347
public class Mouse
{
private static int mouseCount;
private static Mouse4 youngestMouse;
private static double averageLifeSpan = 18; // months⎫ ⎬
private static int simulationDuration = 730; // days ⎭ private static String researcher; ⎫
⎬ private static boolean noiseOn; ⎭
...
Initializations are allowed.
attributes of the environment
Figure 9.1 Class variable declarations in an enhanced Mouse class