HuntExams Academy logo
JavaIntermediate#puzzle#oop

What is the output of this code involving static and instance initialization order? class A { static { System.out.println("static A"); } { System.out.println("instance A"); } A() { System.out.println("constructor A"); } } new A(); new A();

Answer

Static blocks run once when the class is first loaded, before any instance is created. Instance blocks run every time a new object is created, right before the constructor body.

Example

new A(); new A();
// Output:
// static A
// instance A
// constructor A
// instance A
// constructor A

Detailed explanation

Assuming the two new A() statements appear inside a valid method or another executable context, and A has not already been initialized, the output is exactly:\nstatic A\ninstance A\nconstructor A\ninstance A\nconstructor A. Each item appears on its own line because the code uses System.out.println. The static initializer executes once for this class, while the instance initializer and constructor body execute for each of the two objects. The second allocation therefore repeats only the last two messages, not the static message.

The precise Java rule is that a static initializer runs during class initialization, not simply when the class is loaded. Loading, linking, and initialization are distinct lifecycle stages, and a JVM can load a class without immediately executing its static blocks. Creating the first A object is an active use that triggers initialization if necessary. Before A is initialized, its superclass must be initialized. Static field initializers and static blocks then execute in source order within A. Here, there is only one static block, so it prints static A.

For each object, instance fields initially receive their default values. The A constructor implicitly invokes super(), which calls the Object constructor in this example. After that superclass constructor returns, A's instance field initializers and instance initializer blocks execute in source order. The instance block prints instance A, and the remaining A constructor body then prints constructor A. The second new A() creates a separate object and follows the same instance-initialization sequence, but A's class initialization has already completed successfully and is not repeated.

Interviewers generally expect both the output and an explanation of the separate class-level and object-level lifecycles. Saying that the instance block runs before the constructor is useful shorthand but incomplete: it runs as part of construction, after superclass construction and before the remaining constructor body. Also, once applies to a particular runtime class identity; independently defined copies of A in different class loaders can each be initialized. These distinctions matter when reasoning about inheritance, shared static state, and constructor side effects.

Key points to remember

  • Output order: static A → instance A → constructor A → instance A → constructor A, each on a separate line.
  • The first new A() triggers class initialization if A is not already initialized.
  • Static initialization occurs once per runtime class identity, not once per object.
  • Instance initializers execute for each object after superclass construction and before the remaining constructor body.
  • Static initializers follow source order among themselves; instance initializers follow source order among themselves.

Common mistakes candidates make

  • Claiming that class loading always immediately executes static blocks.
  • Printing static A twice because there are two new A() expressions.
  • Saying that instance initializers run before superclass construction.

Likely follow-up questions

What changes if A has already been initialized?

These two statements will not print static A again. They will print instance A followed by constructor A twice.

What happens if one constructor calls another using this()?

The instance initializers still execute only once for that object. They execute in the constructor chain after superclass construction, before the body of the constructor that called super() continues.

How does inheritance affect initialization order?

On first active use, superclass static initialization precedes subclass static initialization. During construction, superclass instance initializers and its constructor body run before subclass instance initializers and its constructor body.

Interview tip: State the five output lines first, then explain the once-per-class versus once-per-object distinction and clarify the role of super().

Related Interview Questions

1
JavaIntermediate#puzzle#fundamentals

What does this print? Integer a = 127; Integer b = 127; Integer c = 128; Integer d = 128; System.out.println(a == b); System.out.println(c == d);

Open
2
JavaBeginner#puzzle#operators

What is the output? public class Test { public static void main(String[] args) { System.out.println(10 / 3); System.out.println(10.0 / 3); System.out.println(10 % 3); } }

Open
3
JavaAdvanced#puzzle#oop

What happens when you compile and run this code? class Parent { static void greet() { System.out.println("Parent"); } } class Child extends Parent { static void greet() { System.out.println("Child"); } } Parent p = new Child(); p.greet();

Open