Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Before a restaurant opens each morning, they do one-time setup — clean tables, stock kitchen, turn on lights. This setup happens ONCE when the restaurant opens, not every time a customer walks in. That's a static block.
What is a Static Block?
A static block executes AUTOMATICALLY and ONCE when the class is loaded into JVM memory — before any object is created or any method is called.
java
QUBITS OF DPK
Execution Order
javascript
QUBITS OF DPK
Dry Run Example
java
QUBITS OF DPK
Output:
javascript
QUBITS OF DPK
Multiple Static Blocks
java
QUBITS OF DPK
Static Block vs Constructor
Production Use Cases
java
QUBITS OF DPK
️ All Traps
java
QUBITS OF DPK
Interview Answer (SDE-2)
"A static block is a one-time initialization block that executes automatically when the class is loaded into JVM — before main method and before any object is created. It runs exactly once regardless of how many objects are created. Multiple static blocks execute in top-to-bottom order. Used for class-level setup like loading configuration, registering JDBC drivers, or initializing complex static data structures. Key distinction: constructor runs per object, static block runs per class load."
Interview Questions & MAANG-Level Answers
Q1. When does a static block execute?
A static block executes when the class is loaded into JVM memory — before any object is created, before main() runs (even if static block is in the same class), before any static method is called. Class loading happens the first time the class is referenced: first object creation, first static method call, or first static field access.
Q2. How many times does a static block run?
Exactly once per class load — regardless of how many objects you create. If you create 1000 objects, the static block still runs only once. The JVM guarantees this. Multiple static blocks in the same class all run once each, in top-to-bottom order.
Q3. What is the difference between static block and constructor?
Static block: runs once when class loads, before any object, no this reference, used for class-level one-time setup (load config, register drivers). Constructor: runs every time an object is created with new, has this reference, used for object-level initialization. Execution order: static variables init → static block → ... → new called → instance variables init → constructor body.
Q4. Can a static block access instance variables?
No. Static block runs before any object exists, so there's no this reference and no instance variables to access. static { System.out.println(this.name); } — compile error. Static blocks can only access static variables and static methods. To work with instance data, you must create an object inside the static block: static { MyClass obj = new MyClass(); System.out.println(obj.name); }.
Q5. If a class has both a static block and main(), which runs first?
The static block runs FIRST, even if it's defined after main():
java
QUBITS OF DPK
This is because the JVM loads the class (triggering static block) before calling main(). Classic interview trap!