1.5 Static Members

In some cases, certain members should only belong to the class, and not be part of any object created from the class. An example of such a situation is when a class wants to keep track of how many objects of the class have been created. Defining a counter as an instance variable in the class definition for tracking the number of objects created, does not solve the problem. Each object created will have its own counter field. Which counter should then be updated? The solution is to declare the counter field as being static. Such a field is called a static variable. It belongs to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime. Similarly, a class can have static methods that belong only to the class, and not to any objects of the class. Static variables and static methods are collectively known as static members, and are distinguished from instance members in a class definition by the keyword static in their declaration.

Figure 1.4 shows the class diagram for the class CharStack. It has been augmented by two static members that are shown underlined. The augmented definition of the CharStack class is given in Example 1.2. The field counter is a static variable declared at (1). It will be allocated and initialized to the default value 0 when the class is loaded. Each time an object of the CharStack class is created, the constructor at (2) is executed. The constructor explicitly increments the counter in the class. The method getInstanceCount() at (3) is a static method belonging to the class. It returns the counter value when called.

Figure 1.4. Class Diagram Showing Static Members of a Class


Figure 1.5 shows the classification of the members in class CharStack using the terminology we have introduced so far. Table 1.1 at the end of this section, provides a summary of the terminology used in defining members of a class.

Example 1.2 Static Members in Class Definition
// Source Filename CharStack.java
public class CharStack {
    // Instance variables
    private char[] stackArray;  // The array implementing the stack.
    private int    topOfStack;  // The top of the stack.

    // Static variable
    private static int counter;                                      // (1)

    // Constructor now increments the counter for each object created.
    public CharStack(int capacity) {                                 // (2)
        stackArray = new char[capacity];
        topOfStack = -1;

    // Instance methods
    public void push(char element) { stackArray[++topOfStack] = element; }
    public char pop()              { return stackArray[topOfStack--]; }
    public char peek()             { return stackArray[topOfStack]; }
    public boolean isEmpty()       { return topOfStack < 0; }
    public boolean isFull()        { return topOfStack == stackArray.length - 1; }

    // Static method                                                    (3)
    public static int getInstanceCount() { return counter; }
Figure 1.5. Members of a Class


Clients can access static members in the class by using the class name. The following code invokes the getInstanceCount() method in the class CharStack:

int count = CharStack.getInstanceCount(); // Class name to invoke static method

Static members can also be accessed via object references:

CharStack stack1 = new CharStack(10);
int count1 = stack1.getInstanceCount();   // Reference invokes static method

Static members in a class can be accessed both by the class name and via object references, but instance members can only be accessed by object references.

Table 1.1. Terminology for Class Members

Instance Members

These are instance variables and instance methods of an object. They can only be accessed or invoked through an object reference.

Instance Variable

A field that is allocated when the class is instantiated, that is, when an object) of the class is created. Also called non-static field.

Instance Method

A method that belongs to an instance of the class. Objects of the same class share its implementation.

Static Members

These are static variables and static methods of a class. They can be accessed or invoked either by using the class name or through an object reference.

Static Variable

A field that is allocated when the class is loaded. It belongs to the class and not to any object of the class. Also called static field and class variable.

Static Method

A method which belongs to the class and not to any object of the class. Also called class method.