Static members in Java include static variables and static methods. They are shared among all instances of a class and can be accessed without creating an instance of the class.

Static Variables

Static variables, also known as class variables, are shared among all instances of a class. They are initialized only once, at the start of the execution.

Example

  public class Counter {
    static int count = 0; // Static variable

    // Method to increment the count
    public void increment() {
        count++;
    }

    // Method to display the count
    public void display() {
        System.out.println("Count: " + count);
    }

    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();

        c1.increment();
        c2.increment();

        c1.display(); // Output: Count: 2
        c2.display(); // Output: Count: 2
    }
}
  

Explanation

  • static int count is a static variable. It is shared among all instances of the Counter class.
  • Both c1 and c2 share the same count variable. Changes made to count through one instance are visible to all other instances.

Static Methods

Static methods belong to the class rather than to any instance. They can be called without creating an instance of the class.

Example

  public class MathUtils {
    // Static method to find the maximum of two numbers
    public static int max(int a, int b) {
        return (a > b) ? a : b;
    }

    public static void main(String[] args) {
        int result = MathUtils.max(10, 20);
        System.out.println("Maximum: " + result); // Output: Maximum: 20
    }
}
  

Explanation

  • public static int max(int a, int b) is a static method. It can be called directly using the class name (MathUtils.max(10, 20)).
  • Static methods cannot access instance variables or methods directly; they can only access static variables and methods.

Static Block

A static block is used to initialize static variables. It is executed only once, when the class is first loaded.

Example

  public class InitializationDemo {
    static int value;

    // Static block
    static {
        value = 10;
        System.out.println("Static block executed.");
    }

    public static void main(String[] args) {
        System.out.println("Value: " + value);
    }
}
  

Explanation

  • The static block initializes the static variable value and is executed when the class is loaded.
  • The message “Static block executed.” is printed when the class is loaded, and “Value: 10” is printed when the main method runs.

Accessing Static Members

Static members can be accessed directly using the class name or through an instance of the class (though it’s not recommended).

Example

  
public class Example {
    static int staticVariable = 100;

    public static void staticMethod() {
        System.out.println("Static method called.");
    }

    public static void main(String[] args) {
        // Accessing static members using class name
        System.out.println("Static Variable: " + Example.staticVariable);
        Example.staticMethod();

        // Accessing static members through an instance (not recommended)
        Example obj = new Example();
        System.out.println("Static Variable: " + obj.staticVariable);
        obj.staticMethod();
    }
}
  

Explanation

  • Static members can be accessed using the class name (Example.staticVariable and Example.staticMethod()).
  • Although it’s possible to access static members through an instance (obj.staticVariable and obj.staticMethod()), it’s not recommended as it may cause confusion.

Example Program

Here’s a complete example demonstrating the use of static variables, static methods, and static blocks:

  public class Library {
    static String libraryName = "Central Library";
    static int bookCount;

    // Static block to initialize static variables
    static {
        bookCount = 1000;
        System.out.println("Static block executed. Library initialized.");
    }

    // Static method to display library details
    public static void displayLibraryInfo() {
        System.out.println("Library Name: " + libraryName);
        System.out.println("Number of Books: " + bookCount);
    }

    public static void main(String[] args) {
        // Displaying library information using static method
        Library.displayLibraryInfo();

        // Changing static variable through class name
        Library.bookCount = 1200;

        // Displaying updated library information
        Library.displayLibraryInfo();
    }
}
  

Explanation

  • The static block initializes bookCount and prints a message when the class is loaded.
  • The static method displayLibraryInfo prints the libraryName and bookCount.
  • The main method demonstrates accessing and modifying static variables and methods.