What is the output of the below program?
public class Sequence {
Sequence() {
System.out.print("c ");
}
{
System.out.print("y ");
}
public static void main(String[] args) {
new Sequence().go();
}
void go() {
System.out.print("g ");
}
static {
System.out.print("x ");
}
}
Before you scroll down, write your answer. Most developers guess c y x g or x c y g. Both are wrong.
The correct output is: x y c g
Here is exactly why each letter prints in that order.
Step 1: x — the static block runs first
The static { } block runs once when the JVM loads the Sequence class. This happens before main starts, before any object is created. The block is written at the bottom of the source file, but Java does not care about visual position. Class loading fires it first.
Step 2: y — the instance initializer runs next
When new Sequence() executes, the JVM allocates the object and prepares to call the constructor. Before the constructor body runs, every instance initializer block executes. The { System.out.print("y "); } block is an instance initializer. The compiler copies it into every constructor, right after the implicit super() call.
Step 3: c — the constructor body runs
Only after the instance initializer finishes does the Sequence() constructor body execute. It prints c.
Step 4: g — the method call runs last
The object is fully constructed. go() is invoked and prints g.
Final output: x y c g
The four rules behind this order
| Order | What runs | When | How many times |
|---|---|---|---|
| 1 | Static initializer block | When class is loaded | Once per class loader |
| 2 | Instance initializer block | Before constructor body, after super() |
Every object creation |
| 3 | Constructor body | After instance initializers | Every object creation |
| 4 | Method call | When explicitly invoked | As called |
Follow-up: what if Sequence extends another class?
Interviewers rarely stop at the basic output. If Sequence extended a parent class Base, the order becomes:
- Parent static block
- Child static block
- Parent instance initializer
- Parent constructor
- Child instance initializer
- Child constructor
- Method call
The rule is simple: static members initialize from parent to child first, then instance members initialize from parent to child.
Why interviewers love this question
This single program tests four separate concepts at once:
- Class loading vs object instantiation. Can you separate what happens once per class from what happens per object?
- Static initialization timing. Do you know that static blocks run before
main? - Initializer block semantics. Do you know that instance initializers run before constructor bodies, not after?
- Source order vs execution order. The static block is at the bottom. Does that change anything? (No, for static blocks. Yes, for multiple instance blocks in the same class.)
A developer who gets this wrong likely does not understand the JVM lifecycle well enough to debug class-loading issues or write thread-safe singletons.
Where to go next
You now know why the output is x y c g. You know the inheritance variant. You know the rules.
If you want to go deeper, try this: add a static variable assignment before the static block, then reference that variable from main. Watch what changes. Or write a class with two constructors and one instance initializer. See how the initializer runs before both. The pattern will stick faster when you type it yourself.
And the next time someone shows you a "simple" output question, check the blocks first. The JVM has rules, and now you know them.
No comments:
Post a Comment