Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Class Static Blocks
How and Why They Are Used



What is a Static Block in Java?

In Java, a static block (also known as a static initialization block) is a code block that's executed once when the class is loaded into memory. It's mostly used to initialize static variables or execute code that must run before any objects of the class are created.

Why Use a Static Block?

Static blocks are especially useful when:

Syntax of a Static Block

class MyClass {
    static {
        // This code runs once when the class is loaded
    }
}

Example 1: Basic Static Block Execution

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

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

Explanation:

When you run the class, the JVM loads it and executes the static block first. After that, it invokes the main() method.

Example 2: Initializing Static Variables

public class Config {
    static int MAX_USERS;
    static String APP_NAME;

    static {
        MAX_USERS = 100;
        APP_NAME = "MyApp";
        System.out.println("Static block: Configuration loaded");
    }

    public static void main(String[] args) {
        System.out.println("App: " + APP_NAME);
        System.out.println("Max users: " + MAX_USERS);
    }
}
Static block: Configuration loaded
App: MyApp
Max users: 100

Explanation:

The static block sets up configuration data for the class. It's run only once and the values are available for use throughout the application wherever static members are accessible.

Example 3: Multiple Static Blocks

public class MultiStaticBlocks {
    static {
        System.out.println("Static block 1");
    }

    static {
        System.out.println("Static block 2");
    }

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

Explanation:

If there are multiple static blocks, they execute in the order they appear in the class, from top to bottom, during class loading.

Example 4: No Object Required

public class NoObjectNeeded {
    static {
        System.out.println("Class is loaded!");
    }

    public static void hello() {
        System.out.println("Hello!");
    }
}
// Another class
public class Test {
    public static void main(String[] args) {
        NoObjectNeeded.hello();
    }
}
Class is loaded!
Hello!

Explanation:

Even though we didn’t create an object of NoObjectNeeded, calling its static method triggered class loading, which caused the static block to run. Static blocks belong to the class itself, not to any instance.

Points to Remember

Common Use Cases

Interview Tip

Static blocks are a common favorite in Java interviews. You might be asked, "What happens if there's a static block but no main method?" Answer: the static block still runs when the class is loaded, but you'll get a runtime error unless a valid main() method is found to continue execution.

Conclusion

Static blocks might feel like backstage workers in Java – quiet, behind the scenes, but essential. Whether you're preloading values or just setting the stage before the curtain rises (a.k.a. main()), understanding static blocks is a fundamental step toward mastering class behavior and lifecycle in Java.

QUIZ

Question 1:When is a static block in Java executed?

Question 2:A static block can be used to initialize static variables in Java.

Question 3:What are the valid uses of static blocks in Java?

Question 4:What will be the output of the following code?
public class Example {
    static {
        System.out.println("Static block");
    }
    public static void main(String[] args) {
        System.out.println("Main method");
    }
}

Question 5:Static blocks require an object of the class to be executed.

Question 6:What will be printed when the following Java code is executed?
public class Blocks {
    static {
        System.out.println("First block");
    }
    static {
        System.out.println("Second block");
    }
    public static void main(String[] args) {
        System.out.println("Main method");
    }
}



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M