In Java, the static keyword is primarily used for memory management. It can be applied to variables, methods, blocks, and nested classes. During the JVM class loading process, JVM class loader subsystem first looks for static members and executes static blocks and initialise static variables as part of the class initialisation stage (learn more on Initialisation).

Static variable

Static variable also known as class variable, it is shared among all instances of the class. They are initialised only once when the class is loaded to the memory and stored in the JVM method area.

public class Example {
    static int count = 0; // static variable
    
    Example() {
        count++;
        System.out.println(count);
    }
    
    public static void main(String args[]) {
        Example obj1 = new Example(); 
        Example obj2 = new Example();
    }
}
1
2

Static method

Static method is belong to a class rather than the instances of the class. They can be called without creating an instance of the class. Static methods can access static variables and methods directly but cannot access instance variables and methods directly.

Important

We cannot override or overload a static method, the static resolves against the class, not the instance.

public class Example {
    static int count = 0;
    int value = 0;
    
	// static method
    static void incrementCount() { 
        count++;
    }
	static void printValue() {
		// this will cause an error, static method can't access instance variable
		System.out.println("Value is: " + value);
	}
    
    public static void main(String args[]) {
        Example.incrementCount();
        System.out.println(Example.count); 
    }
}
 
1

To access non-static variable in static method, use dependency injection or static dependency. Dependency injection

public class Example {
	int value = 0;
	
	static void printValue(Example obj) {
		System.out.println("Value is: " + value);
	}
	
	public static void main(String args[]) {
		Example myExample = new Example();
		Example.printValue(myExample);
	}
}

Static dependency

public class Example {
	int value = 0;
	
	static void printValue() {
		Example myExample = new Example();
		System.out.println("Value is: " + myExample.value);
	}
	
	public static void main(String args[]) {
		Example.printValue();
	}
}

Static block

Code inside a static block is executed only only once when the class is first loaded by JVM. It is often used to initialise static members.

Note

The static blocks are executed before main method invocation. This is discussed in the JVM class loader subsystem.

public class Example {
    static int count;
    
	// static block
    static {
        count = 10; 
        System.out.println("Static block initialised.");
    }
    
    public static void main(String args[]) {
        System.out.println(count); 
    }
}
Static block initialised.
10

Static nested class

A static nested class or static inner class is defined inside another class.

  • Different from normal nested class, static nested class can be instantiated without the instance of the outer class.
  • A static nested class can access static members of the outer class, including private.
  • A non-static nested class has full access to the members of the class within which it is nested. A static nested class does not have a reference to a nesting instance, so a static nested class cannot invoke non-static methods or access non-static fields of an instance of the class within which it is nested.
public class OuterClass {
    static int outerStaticVar = 100;
    int outerInstanceVar = 200; 
    
    // static nested class
    static class NestedStaticClass {
        void display() {
            // can access static members of the outer class
            // without instantiate the outer class
            System.out.println("Outer static variable: " + outerStaticVar);
            
            // cannot access non-static (instance) members of the outer class
            // the following line will raise an error
            // System.out.println("Outer instance variable: " + outerInstanceVar); 
        }
    }
    
    public static void main(String[] args) {
        // instantiating the static nested class
        OuterClass.NestedStaticClass nestedObject = new OuterClass.NestedStaticClass();
        nestedObject.display();
    }
}
Outer static variable: 100

To access the instance variable of the outer class, you have to instantiate the outer class object.

public class OuterClass {
	// ...
    static class NestedStaticClass {
        void display() {
	        OuterClass outerInstance = new OuterClass();
	        System.out.println("Outer instance variable: " + outerInstance.outerInstanceVar); 
        }
    }
}

Back to parent page: Java Standard Edition (Java SE) and Java Programming

Web_and_App_DevelopmentProgramming_LanguagesJavaStaticStatic_MethodStatic_VariableStatic_BlockStatic_Nested_ClassNestedClass