Why Java Runs Static Blocks Before Constructors: A Deep Dive into Initialization Order
This article explains Java's class initialization sequence, showing how static variables, static blocks, constructors, and the main method are executed in single‑class and multi‑inheritance scenarios, and demonstrates how to control static fields for specific subclasses during testing.
Simple class execution order
package practice;
public class Cbc {
public static Cbc cbc = new Cbc();
public static void main(String[] args) {
System.out.println("Entering program entry!");
}
public Cbc() {
System.out.println("I am Cbc constructor!");
}
static {
System.out.println("I am Cbc static block!");
}
}I am Cbc constructor! I am Cbc static block! Entering program entry!
Execution order: static variable initialization, then static block, then the main method.
Multiple inheritance scenario
package practice;
public class Cbc extends Bbc {
public static Cbc cbc = new Cbc();
public static void main(String[] args) {
System.out.println("Entering program entry!");
}
public Cbc() {
System.out.println("I am Cbc constructor!");
}
static {
System.out.println("I am Cbc static block!");
}
}
class Bbc extends Abc {
public static Bbc bbc = new Bbc();
public Bbc() {
System.out.println("I am Bbc constructor!");
}
static {
System.out.println("I am Bbc static block!");
}
}
class Abc {
public static Abc abc = new Abc();
public Abc() {
System.out.println("I am Abc constructor!");
}
static {
System.out.println("I am Abc static block!");
}
}I am Abc constructor! I am Abc static block! I am Abc constructor! I am Bbc constructor! I am Bbc static block! I am Abc constructor! I am Bbc constructor! I am Cbc constructor! I am Cbc static block! Entering program entry!
The output demonstrates that initialization proceeds from the top of the hierarchy (Abc) down through Bbc to Cbc. For each class, the static variable is created first, then its static block runs, and finally its constructors are invoked.
Modifying static fields in an inheritance chain
If a base class defines a static field, e.g., public static int num = 1; in Abc, a subclass can change its value in a static block. Setting num = 2 in Bbc 's static block makes the value 2 visible to Cbc and any other subclasses of Bbc, while code paths that do not involve Bbc continue to see the original value 1.
Test code demonstrating static field change
public class Cbc extends Bbc {
public static void main(String[] args) {
System.out.println("Entering program entry!");
System.out.println("num value: " + num);
}
}
class Bbc extends Abc {
static {
num = 2;
}
}
class Abc {
public static int num = 1;
}Entering program entry! num value: 2
This confirms that the static block in Bbc overrides the inherited static field, affecting only execution paths that involve Bbc and its subclasses.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
