CIT 591 Static vs. Instance
Fall 2006, David Matuszek

Students always have trouble with static variables and methods. Here's yet another attempt to explain the differences.

First, here are some things you can and cannot do:

class SomeClass {
    int myID;
    static int nextID = 0;
 
    public SomeClass() { // constructor
        myID = nextID++;
    }
 
    void someStaticMethod(int n) {
        n = 5;
        myID = 5;  // illegal
        nextID = 5;
        someStaticMethod(5);
        someInstanceMethod(5); //illegal
    }

    void someInstanceMethod(int n) {
        n = 5;
        myID = 5; // same as this.myID
        nextID = 5;
        someStaticMethod(5);
        someInstanceMethod(5);
    }
}
class SomeOtherClass {
 
    void aStaticMethod() {
        SomeClass.myID = 5; // illegal
        SomeClass.nextID = 5;
        SomeClass.someStaticMethod(5);
        SomeClass.someInstanceMethod(5); //illegal

        SomeClass thing = new SomeClass();
        thing.myID = 5;
        thing.nextID = 5;
        thing.someStaticMethod(5);
        thing.someInstanceMethod(5);
    }

    void anInstanceMethod() {
        // same as in a static method
    }
}

Classes describe objects. A class can describe many objects, all of the same type (type=class). Even when a program needs only one object of that class, it's usually a good idea to write the class so as to allow many objects of that type; this simplifies the code and gives you greater flexibility for the future.

Static variables belong to the class itself, not to objects of the class. There is only one instance of each static variable, and it is shared among all the objects of that class. Different objects cannot have different values for a static variable. Hence, static variables are very limited, and should generally be avoided.

Every object (instance) of a class has its own copy of each instance variable declared in the class. The instance variables (also called fields) are supposed to describe that particular object. Instance variables are not intended as a dumping ground for assorted variables that are used in the program, but do not describe the object.

Local variables are used for values that are used in computation, but don't describe the object in any way. If a method needs a variable that is declared in some other method, it should be passed in as a parameter, not made into a field.

For example, suppose you have a Game class. Is it reasonable ever to have more than one game? Not just in this one immediate program you are writing today, but in programs in general? Yes, it is. Hence, the information describing the game should be in instance variables of the game.

Suppose you are writing an EquationSolver class to solve algebraic equations. There doesn't seem to be much point to having more than one equation solver (since they would all be the same anyway), so it's reasonable to make more things static. Of course, you would be working with more than one Equation, and every Equation should be its own object, containing a complete description of itself.

Instance methods represent actions that an object can take. They almost always read or change instance variables of the object to which they belong. If a method makes no use of the information unique to a particular object, it should probably be a static method.

An instance method can be called in two ways:

For example, suppose you want to choose the best move to make in a game. This will depend heavily on the current state of the game. It should be an instance method.

Suppose you have a method that finds the largest value in an array. The array can be passed in as a parameter. This method makes no use of instance variables, and in fact could be in any class whatsoever. It should be a static method.

Static methods can directly use static variables and other static methods. They can use instance variables and methods only by making an object (instance) of the correct type and "talking" to it, using dot notation.

If you have a static method that tries to call an instance method, don't go and change the instance method to be static--unless you decide that that method really should be static. Instead, either:

So:

Finally, here are some guidelines (not absolute rules).