Static in Java

1. Static Keyword in Java With Examples

The static keyword in Java is used for memory management mainly. It can be applied to variables, methods, blocks, and nested classes. The static keyword belongs to the class rather than instances of the class.


public class StaticExample {
    static int count = 0; // static variable

    static void displayCount() { // static method
        System.out.println("Count: " + count);
    }

    public static void main(String[] args) {
        StaticExample.displayCount(); // calling static method
    }
}
    

2. Static Method Overloading or Overriding in Java

Static methods can be overloaded but cannot be overridden. Overloading is when you have multiple methods with the same name but different parameters. Overriding is when a subclass provides a specific implementation of a method that is already defined in its superclass.


class Example {
    static void display() {
        System.out.println("Static method in Example class");
    }

    static void display(int a) { // overloading static method
        System.out.println("Overloaded static method with parameter: " + a);
    }
}

class Test extends Example {
    // This will hide the static method in Example class, not override it
    static void display() {
        System.out.println("Static method in Test class");
    }
}

public class Main {
    public static void main(String[] args) {
        Example.display();
        Example.display(10);
        Test.display();
    }
}
    

3. Static Import in Java With Examples

Static import allows you to access static members (fields and methods) of a class directly without using the class name.


import static java.lang.Math.*; // static import

public class StaticImportExample {
    public static void main(String[] args) {
        System.out.println("Square root of 16: " + sqrt(16)); // no need to use Math.sqrt
        System.out.println("PI value: " + PI); // no need to use Math.PI
    }
}
    

4. Static Reference to The Non-static Method or Field Error

This error occurs when you try to access a non-static method or field from a static context. Non-static members belong to instances of the class, while static members belong to the class itself.


public class Example {
    int instanceVar = 10; // non-static field

    void instanceMethod() { // non-static method
        System.out.println("Instance method");
    }

    static void staticMethod() {
        // System.out.println(instanceVar); // Error: Cannot make a static reference to the non-static field
        // instanceMethod(); // Error: Cannot make a static reference to the non-static method
    }
}
    

5. Static Block in Java

A static block is used for static initializations of a class. This code inside the static block is executed only once when the class is first loaded into memory.


public class StaticBlockExample {
    static {
        System.out.println("Static block executed");
    }

    public static void main(String[] args) {
        System.out.println("Main method executed");
    }
}