how2examples.com
Home - Introduction to Java Programming

Java's static keyword

The static keyword can be applied to both variables and methods. Variables and methods marked as static belong to a class rather than an individual instance of a class.

static variables

When a new instance of a class is created that instance has its own unique version of every member variable of the class. Static variables (i.e. variables marked with static keyword) are different - they only exist once for the class regardless of how many instances (i.e. objects) of the class are created. If a object updates a static variable then that change will affect any other objects that subsequentally use that static variable.

Example of how changes to static variable are not limited to a single instace of a class.
Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      for (int i=0; i<3; i++) {
         A a = new A();
         a.i1++;
         a.i2++;
         System.out.println(a.i1+" "+a.i2);
      }
   }
}

class A {
   int i1;
   static int i2;
}
Command to compile Example.java:
javac Example.java
Command to run Example:
java Example
Output from running Example:
1 1
1 2
1 3

In fact, as static variables belong to a class rather than an object, it is not neccassary to create an instance of the class in order to use a static variable. Static variables can be referred by using the name of the class they belong to.

Example of refferring to a static variable by the name of the class it belongs to.
Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      System.out.println(A.i);
   }
}

class A {
   static int i = 9999;
}
Command to compile Example.java:
javac Example.java
Command to run Example:
java Example
Output from running Example:
9999

Note that it is not possible to refer to a member variable by the name of the class it belongs to. As member variables belong to a specific instance of a class the compiler needs to know which particular instance to use.

Example of how it is not possible to refer to a member variable using the name of the class it belongs to.
Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      System.out.println(A.i);
   }
}

class A {
   int i = 9999;
}
Command to compile Example.java:
javac Example.java
Result of trying to compile Example.java:
Example.java:3: non-static variable i cannot be referenced from a static context
      System.out.println(A.i);
                          ^
1 error

static methods

The static keyword can be applied to methods. Like static variables, static methods can be referenced by using the name of the class they belong to. Static methods can refer to other static methods and variables.

Example of calling a static method which refers to a static variable.
Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      A.x();
   }
}

class A {
   static int i = 9999;

   static void x() {
      System.out.println("A.x() "+i);
   }
}
Command to compile Example.java:
javac Example.java
Command to run Example:
java Example
Output from running Example:
A.x() 9999

As static methods are not associated with an individual instance of a class they are not able to directly refer to the member variables or non-static methods of their class.

Example of a static method that tries to refer to a member (i.e. non-static) variable.
Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      A.x();
   }
}

class A {
   int i = 9999;

   static void x() {
      System.out.println("A.x() "+i);
   }
}
Command to compile Example.java:
javac Example.java
Result of trying to compile Example.java:
Example.java:11: non-static variable i cannot be referenced from a static context
      System.out.println("A.x() "+i);
                                  ^
1 error

static initialisation blocks

It is possible for a class to contain static code that is not associated with a method. The code will be executed when the class is first loaded.

Example of a static initialisation block.
Contents of Example.java:
public class Example {
   public static void main(String[] args) {
      new A().x();
      new A().x();
      new A().x();
   }
}

class A {
   static {
      System.out.println("Static initialiser block");
   }
   
   A() {
      System.out.println("Constructor");
   }

   static void x() {
      System.out.println("x()");
   }
}
Command to compile Example.java:
javac Example.java
Command to run Example:
java Example
Output from running Example:
Static initialiser block
Constructor
x()
Constructor
x()
Constructor
x()