Java: “static” Keyword

By Xah Lee. Date: . Last updated: .
  1. static keyword can be used in the declaration of variables and methods.
  2. A variable declared with static is called “class variable”.
  3. A method declared with static is called “class method”.
  4. Variables/methods without static keyword are called “instance variable” and “instance method”.
  5. the static keyword cannot be used in class declaration.

Purpose of class variable and methods are:

Following are details.

Class Variable vs Instance Variable

“static” Keyword = Class Variable

Variables can be declared with the static keyword.

static int y = 0;

When a variable is declared with the keyword static, its called a class variable.

No “static” Keyword = Instance Variable

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

// example of instance vs 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.

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

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

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

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

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

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 V2 {
    int x = 3;
    static int returnIt () { return x;}
}

class V1 {
    public static void main(String[] arg) {
        System.out.println( V2.returnIt() );
    }
}
~ $ javac V1.java
V1.java:3: error: non-static variable x cannot be referenced from a static context
    static int returnIt () { return x;}
                                    ^
1 error
~ $

Java

  1. Install
  2. javac java jar javadoc

Java Basics

  1. Print String
  2. Strings
  3. Power Function
  4. Java Types and Type Conversion
  5. Function
  6. Arrays

Class, Inheritance

  1. extends
  2. Constructor
  3. Constructor Return Type
  4. Extending a Class that has Explicit Constructors
  5. static
  6. this
  7. super
  8. Access Specifiers

Abstract class, Interface

  1. abstract
  2. Interface

Package

  1. Package

Misc

  1. Collection, Map
  2. Read/Write File
  3. Convert File Encoding
  4. Complex Numbers in Java
  5. Unicode
  6. Number Literals