What is "static"?

java, static

Solution

The concept of `static` has to do with whether something is part of a class or an object (instance).

In the case of the `main` method which is declared as `static`, it says that the `main` method is an class method -- a method that is part of a class, not part of an object. This means that another class could call a class method of another class, by referring to the `ClassName.method`. For example, invoking the run method of `MyClass` would be accomplished by:

MyClass.main(new String[]{"parameter1", "parameter2"});

On the other hand, a method or field without the `static` modifier means that it is part of an object (or also called "instance") and not a part of a class. It is referred to by the name of the specific object to which the method or field belongs to, rather than the class name:

MyClass c1 = new MyClass();
c1.getInfo()     // "getInfo" is an instance method of the object "c1"

As each instance could have different values, the values of a method or field with the same name in different objects don't necessarily have to be the same:

MyClass c1 = getAnotherInstance();
MyClass c2 = getAnotherInstance();

c1.value     // The field "value" for "c1" contains 10.
c2.value     // The field "value" for "c2" contains 12.
             // Because "c1" and "c2" are different instances, and 
             // "value" is an instance field, they can contain different
             // values.

Combining the two concepts of instance and class variables. Let's say we declare a new class which contains both instance and class variables and methods:

class AnotherClass {
    private int instanceVariable;
    private static int classVariable = 42;

    public int getInstanceVariable() {
        return instanceVariable;
    }

    public static int getClassVariable() {
        return classVariable;
    }

    public AnotherClass(int i) {
        instanceVariable = i;
    }
}

The above class has an instance variable `instanceVariable`, and a class variable `classVariable` which is declared with a `static` modifier. Similarly, there is a instance and class method to retrieve the values.

The constructor for the instance takes a value to assign to the instance variable as the argument. The class variable is initialized to be `42` and never changed.

Let's actually use the above class and see what happens:

AnotherClass ac1 = new AnotherClass(10);

ac1.getInstanceVariable();             // Returns "10"
AnotherClass.getClassVariable();       // Returns "42"

Notice the different ways the class and instance methods are called. The way they refer to the class by the name `AnotherClass`, or the instance by the name `ac1`. Let's go further and see the behavioral differences of the methods:

AnotherClass ac1 = new AnotherClass(10);
AnotherClass ac2 = new AnotherClass(20);

ac1.getInstanceVariable();             // Returns "10"
AnotherClass.getClassVariable();       // Returns "42"
ac2.getInstanceVariable();             // Returns "20"
AnotherClass.getClassVariable();       // Returns "42"

As can be seen, an instance variable is one that is held by an object (or "instance"), therefore unique to that particular instance, which in this example is the objects referred to by `ac1` and `ac2`.

A class variable on the other hand is only unique to that entire class. To get this point across even better, let's add a new method to the `AnotherClass`:

public int getClassVariableFromInstance() {
    return classVariable;
}

Then, run the following:

AnotherClass ac1 = new AnotherClass(10);
AnotherClass ac2 = new AnotherClass(20);

ac1.getInstanceVariable();             // Returns "10"
ac1.getClassVariableFromInstance();    // Returns "42"
ac2.getInstanceVariable();             // Returns "20"
ac2.getClassVariableFromInstance();    // Returns "42"

Although `getClassVariableFromInstance` is an instance method, as can be seen by being invoked by referring to the instances `ac1` and `ac2`, they both return the same value, `42`. This is because in both instance methods, they refer to the class method `classVariable` which is unique to the class, not to the instance -- there is only a single copy of `classVariable` for the class `AnotherClass`.

I hope that some what clarifies what the `static` modifier is used for.

The Java Tutorials from Sun has a section called Understanding Instance and Class Members, which also goes into the two types of variables and methods.

Problem

I'm beginning to program in Java. ``` public static void main(String[]args) ``` A book said that I should use static in this case, but doesn't clearly say why I should or what it means. Could you clarify this?

Original source

Related problems