JAVA Tutorial



STATIC MEMBERS IN JAVA


Static Members in Java

In Java, static members (variables and methods) belong to the class rather than to any specific instance of the class. This means that static members can be accessed directly by the class name without creating an object of that class.

What are Static Members?

Static members are shared among all instances of a class. These members are initialized when the class is loaded into memory, and they are accessible using the class name.

  • Static Variables: These variables are shared by all instances of a class. A static variable is initialized only once and can be accessed by all instances.
  • Static Methods: These methods can be called without creating an instance of the class. They can access static variables but cannot access instance variables directly.

Why Use Static Members?

Static members are used when we want to share data among all instances of a class, or if we want to define utility methods that don't rely on object instances.

Static Variables

A static variable is common to all instances of a class. When one object modifies the static variable, the change is reflected in all other instances.

class Counter {
    static int count = 0;

    Counter() {
        count++; // Incrementing static variable
    }

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

public class Main {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter.displayCount();  // Output will be 2 as it's shared across all instances
    }
}
  

In the example above, the static variable count is shared across all instances of the Counter class. Each time a new object is created, the static variable is incremented, and the value is shared among all instances.

Static Methods

A static method can be called without creating an object of the class. Static methods can only access static variables and other static methods.

class MathUtils {
    static int add(int a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        int result = MathUtils.add(5, 10); // Calling static method without object
        System.out.println("Sum: " + result);
    }
}
  

In this example, the add() method is static, so it can be called directly using the class name MathUtils, without creating an object.

Rules for Static Members

  • Static variables: They are initialized only once and are shared across all objects of the class.
  • Static methods: They can be called using the class name, and they cannot access non-static (instance) members directly.
  • Static block: Static members can also be initialized in a static block which is executed once when the class is loaded.

Static Block in Java

A static block is used for static initializations of a class. It is executed once, when the class is loaded into memory, before any objects are created.

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

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

In this example, the static block will execute before the main method, demonstrating that static blocks are executed once when the class is loaded.

Accessing Static Members

Static members are accessed using the class name, as they are associated with the class itself rather than with any specific instance.

class Example {
    static int staticVar = 10;
    
    static void staticMethod() {
        System.out.println("Static method called");
    }
}

public class Main {
    public static void main(String[] args) {
        // Accessing static variable and method directly with class name
        System.out.println("Static Variable: " + Example.staticVar);
        Example.staticMethod();
    }
}
  

In this example, we access the static variable and method of the Example class without creating an object.

Important Points to Remember

  • Static variables: Are shared among all objects of the class and are initialized when the class is loaded.
  • Static methods: Can be called without creating an instance of the class and can only access static variables and methods.
  • Static blocks: Are used for initialization and executed once when the class is loaded into memory.

Conclusion

Static members in Java are associated with the class itself rather than with any instance of the class. They provide a way to store and manage data that is shared by all instances of a class. Static methods are particularly useful for utility functions that don’t require instance data, making the code more efficient and reusable.

Quick Tip:

Use static members when you need to share data across all instances of a class, or when you want to define utility methods that don’t depend on object states.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review