Hiprup

What is the use of the static keyword in Java?

The static keyword marks a member as belonging to the class itself rather than to any object. Only one shared copy exists in memory, and it can be accessed directly through the class name without creating an instance. It can be applied to variables, methods, blocks, and nested classes.

  • Static variables — a single copy shared by all objects, useful for counters or shared configuration; with final they become constants such as static final int TIMEOUT = 10;.

  • Static methods — called as ClassName.method() with no object needed; they cannot use this or super and cannot directly touch instance members. main() is static so the JVM can launch the program without an object.

  • Static blocks — execute exactly once, when the class is initialized (first loaded and used), before any object is created — ideal for one-time setup such as loading a properties file.

  • Static nested classes — can be instantiated without an outer-class object, commonly used for helpers and builders.

  • Automation practice — frameworks expose static utilities like ConfigReader.get("url") or wait helpers, but avoid a static WebDriver in parallel TestNG runs; use ThreadLocal<WebDriver> instead.

Key terms: static variable, static method, static block, static nested class, class loading, main(), ThreadLocal

public class StaticDemo {
    static int instanceCount = 0;   // one shared copy for the whole class
    String name;                    // one copy per object

    static {                        // runs once, when the class is initialized
        System.out.println("Static block: class loaded");
    }

    StaticDemo(String name) {
        this.name = name;
        instanceCount++;            // every object updates the same variable
    }

    static void printCount() {      // called without creating any object
        System.out.println("Objects created: " + instanceCount);
    }

    public static void main(String[] args) {
        StaticDemo.printCount();
        new StaticDemo("chromeSession");
        new StaticDemo("firefoxSession");
        StaticDemo.printCount();
    }
}
// Output:
// Static block: class loaded
// Objects created: 0
// Objects created: 2

The static block prints first because the JVM initializes the class before invoking main(), and static blocks run exactly once at that point. The static variable instanceCount is shared by all objects, so each constructor call increments the same single copy, and the count grows from 0 to 2 across two instantiations.

The static method printCount() is invoked through the class name without any object, which is exactly how utility methods are called in test-automation frameworks.

Go beyond the definition: mention that static members are tied to class loading and initialization, that static method calls are bound at compile time to the reference type and therefore cannot be overridden (only hidden), and connect it to your framework by explaining why utility classes use static methods while WebDriver is kept in ThreadLocal for parallel runs. Expect follow-ups like "Why is main static?", "Can a static method access instance variables?" (no, not directly), and "Can we override a static method?".

The classic mistake is saying static means constant — constants require static combined with final.