Why Java Static Blocks Aren’t Executed First
The article explains that Java static blocks are not always the first code to run during class loading because static field initializers and static blocks are executed in the order they appear in the source, and a static field can trigger instance initialization.
Static initialization also has an internal order
Many developers assume that when a class is loaded, all static initialization runs before any instance initialization. While this is true at a coarse-grained level, the JVM actually executes static field initializers and static blocks together, following the exact order they are written in the source file.
Static field initializers and static blocks share the same sequence
During the class‑loading phase, the JVM performs two actions: evaluating static field initializer expressions and executing static initializer blocks. These actions have no intrinsic precedence; they are interleaved according to their textual order. The following example demonstrates this rule:
class Order {
static String s1 = print("one");
static { print("two"); }
static String s2 = print("three");
static String print(String msg) {
System.out.println(msg);
return msg;
}
public static void main(String[] args) {
System.out.println("main");
}
}Running the program prints one, two, three, and main —exactly the order in which the statements appear. The rule “source order = execution order” also applies to static blocks.
Object creation can occur inside static initialization
A static field initializer may contain any expression, including new object creation. Consequently, an object can be instantiated while the class is still being loaded, causing its instance initializer to run in the middle of static initialization.
class Early {
static Helper helper = new Helper();
static { System.out.println("Early static block"); }
public static void main(String[] args) { System.out.println("main"); }
}
class Helper {
{ System.out.println("Helper instance initializer"); }
}Executing Early produces the output:
Helper instance initializer
Early static block
mainThe first line comes from the instance initializer of Helper, which runs because the new Helper() expression appears before the static block in the source.
Returning to the original question
In the original Test class, the three printed lines are explained as follows:
Class‑loading step 1: evaluate the static field static Test test = new Test();. The new Test() creates an object, invoking its instance initializer, which prints normal.
Class‑loading step 2: execute the static initializer block, printing static.
Execution of main: a second new Test() creates another object, causing the instance initializer to run again and print normal.
Thus the first normal does not contradict the rule; it originates from the static field’s new Test() expression, which is evaluated before the static block.
Additional note
The local variable test inside main shadows the static field of the same name, but this name hiding is unrelated to the execution order.
Conclusion
The keyword static determines the phase (class loading) in which initialization occurs, but it does not dictate the internal ordering of that phase. Within static initialization, static field initializers and static blocks run strictly in the order they appear in the source file. If a static field initializer creates an object, the object's instance initialization interleaves with static initialization, producing output that may appear to precede the static block.
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.
samdeepthink
Knowledge Planet: Old Dock's Tech Chronicles Zhihu: SamDeepThinking A technical manager who still codes heavily on the front line. From junior developer to tech lead, then tech manager, now leading the whole front‑ and back‑end development team—leveling up along the way. I have some insights on programming, career development, and tech management.
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.
