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

                346 Chapter 9 Classes with Class Members
For another example, think about the Math class. Its members, like Math.round and Math.PI, are class members because they are associated with the Math class as a whole. In Chapter 5, you learned how to access and use the Math class’s class members. In this chapter, you learn how to implement your own class members.
We start the chapter by showing you how to implement your own class variables. We then show you how to implement class methods, and we use class variables within those methods. Next, we discuss class constants, which are class variables that use the final modifier. After that we present a utility class—a class with general-purpose functionality that other classes can easily use. In the last part of the chapter, you’ll see different kinds of instance and class members brought together in two complete programs. The second of those programs does more than just provide another example with instance and class members. It implements an important data structure called a linked list, which allows you to dynamically create an arbitrarily large number of chained-together objects.
9.2 Class Variables
You already know that class variables are variables that are associated with a class as a whole. In this sec- tion, you’ll learn more details about class variables such as how to declare them, when to use them, what their default values are, and what their scope is. In the next section, you’ll see examples of using class vari- ables from within class methods.
Class Variable Declaration Syntax
 To make a variable a class variable, use the static modifier in its declaration. The static modifier is Apago PDF Enhancer
why many programmers use the term “static variable” when talking about class variables. Likewise, since class constants and class methods also use the static modifier, many programmers use the terms static constant and static method. We’ll stick with the terms class variable, class constant, and class method since those are the terms that Sun uses.
Here is the syntax for a class variable declaration statement:
Should class variables be public or private? The philosophy on this is the same as it is for instance variables. Since you can always write public get/set class methods, you don’t need public class vari- ables any more than you need public instance variables. It’s best to keep your variables as private as possible to maintain control over how they are accessed. Therefore, in addition to making instance variables private, you should also make class variables private.
Why the Term “static”?
As you know, when the Java Virtual Machine (JVM) sees the new operator in a program, it instantiates an object for the specified class. In so doing, it allocates memory space for all of the object’s instance variables. Later, the garbage collector might deallocate (take away) that memory space before the program stops if all references to that space disappear. That sort of memory management, done while the program runs, is called dynamic allocation. Class variables are different. The JVM allocates space for a class variable when the program starts, and that class-variable space remains allocated as long as the program runs. That sort of memory management is called static allocation. That’s why class variables are called static.
<type> <variable-name>;
private static int mouseCount; // total number of mouse objects
<private-or-public> And here is an example:
static



















































































   378   379   380   381   382