The “static” Keyword in Java

, , …,

Class Variable vs Instance Variable

“static” Keyword = Class Variables

Variables can be declared with the “static” keyword. Example:

static int y = 0;

When a variable is declared with the keyword “static”, its called a “class variable”. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance.

No “static” Keyword = Instance Variables

Without the “static” keyword, it's called “instance variable”, and each instance of the class has its own copy of the variable.

In the following code, the class “T2” has two variables x and y. The y is declared with the keyword “static”. In the main class “T1”, we try to manipulate the variables x and y in “T2”, showing the differences of instance variable and class variable.

class T2 {
    int x = 0; // instance variable
    static int y = 0; // class variable

    void setX (int n) { x = n;}
    void setY (int n) { y = n;}

    int getX () { return x;}
    int getY () { return y;}
}

class T1 {
    public static void main(String[] arg) {
        T2 b1 = new T2();
        T2 b2 = new T2();

        b1.setX(9);
        b2.setX(10);

        // each b1 and b2 has separate copies of x
        System.out.println( b1.getX() );
        System.out.println( b2.getX() );

        // class variable can be used directly without a instance of it.
        //(if changed to T2.x, it won't compile)
        System.out.println( T2.y );
        T2.y = 7;
        System.out.println( T2.y );

        // class variable can be manipulated thru methods as usual
        b1.setY(T2.y+1);
        System.out.println( b1.getY() );
    }
}

Instance Method vs Class Methods

Methods can also be declared with the keyword “static”. When a method is declared static, it can be used without having to create a object first. For example, you can define a collection of math functions in a class, all static, using them like functions. Example:

// A class with a static method
class T2 { static int triple (int n) {return 3*n;}}

class T1 {
    public static void main(String[] arg) {

        // calling static methods without creating a instance
        System.out.println( T2.triple(4) );

        // calling static methods thru a instance is also allowed
        T2 x1 = new T2();
        System.out.println( x1.triple(5) );
    }
}

Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”.

Static Methods Cannot Access Non-Static Variables

Methods declared with “static” cannot access variables declared without “static”. The following gives a compilation error, unless x is also static.

class T2 {
    int x = 3;
    static int returnIt () { return x;}
}

class T1 {
    public static void main(String[] arg) {
        System.out.println( T2.returnIt() );
    }
}

Note: There's no such thing as static classs. “static” in front of class creates compilation error. However, there's a similar idea called abstract classes, with the keyword “abstract”. Abstract classes cannot be initialized. See: The “abstract” Keyword in Java.

blog comments powered by Disqus