Quick revision
The short answer
new B() prints "A1 A B ". B() starts with an implicit super(), which calls A(); A() starts with this(1), which runs A(int) after its own implicit super() to Object, printing "A1 "; then the rest of A() prints "A ", and finally the rest of B() prints "B ". In classic Java a this(...) or super(...) call must be the first statement of a constructor; Java 25 relaxes that with flexible constructor bodies, allowing statements before it that do not use the instance. If A had no no-arg constructor, B() would not compile without an explicit super(...) call.
What to remember
In this answer, we followed new B() and saw that it prints "A1 A B ": B() implicitly calls super(), A() chains to A(int) with this(1), A(int) implicitly calls Object(), and then the constructor bodies run on the way back down. A constructor makes exactly one constructor call, and if a superclass has no accessible no-arg constructor, the subclass must call super(...) explicitly. Classic Java required that call to be the first statement, while Java 25's flexible constructor bodies allow prologue statements that do not use the instance.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
new B() prints "A1 A B ". B() starts with an implicit super(), which calls A(); A() starts with this(1), which runs A(int) after its own implicit super() to Object, printing "A1 "; then the rest of A() prints "A ", and finally the rest of B() prints "B ". In classic Java a this(...) or super(...) call must be the first statement of a constructor; Java 25 relaxes that with flexible constructor bodies, allowing statements before it that do not use the instance. If A had no no-arg constructor, B() would not compile without an explicit super(...) call.
Why this question is asked
Constructor chaining decides the order in which an object is built, and that order matters whenever a constructor validates input, sets defaults or calls overridable methods. Interviewers use this short puzzle to check whether we know the hidden calls the compiler adds, how this(...) and super(...) work together, and what the rules about their position are, including the change in Java 25. In this answer, we will recap the two kinds of constructor calls, follow new B() step by step, see what happens when no no-arg constructor exists, and look at the flexible constructor bodies that Java 25 introduced.
1A Quick Recap: this(...) and super(...)
Every constructor starts by calling exactly one other constructor. this(...) calls another constructor of the same class, which is how we chain constructors to avoid repeating code. super(...) calls a constructor of the superclass, which makes sure the parent part of the object is set up before the child part.
If a constructor does not start with either, the compiler inserts super() for us, a call to the superclass's no-argument constructor. So every chain eventually reaches a super call and climbs the hierarchy up to Object.
2Following new B() Step by Step
Here is the code from the question, with the calls the compiler adds written in as comments.
9 messages in order. Play them, or step through them one at a time.
Calls go up the chain first; the bodies run on the way back down.
The call new B() runs B(). Its first, hidden statement is super(), so control goes to A() before B prints anything. A() starts with this(1), so control goes to A(int) before A() prints anything. A(int) has a hidden super() that runs Object's constructor, which does nothing visible. Then A(int) prints "A1 " and returns.
Now the stack unwinds in reverse order. A() continues after this(1) and prints "A ". It returns to B(), which continues after super() and prints "B ". So the output is A1 A B, with a trailing space after each part.
3Why the Rule Exists: Parents Are Built First
The pattern, call up first and run bodies on the way down, guarantees that by the time a subclass constructor body runs, every superclass constructor has finished. B's code can rely on A's fields being initialised, because A's constructors already ran. Each class also needs exactly one path to super: a constructor that starts with this(...) delegates that job to the constructor it calls, so A() does not call super() itself; A(int) does.
4When the Superclass Has No No-Arg Constructor
The implicit super() only works if the superclass has a no-argument constructor that the subclass can access. Suppose A only declares A(int x).
Declaring any constructor in A removes the default no-argument constructor the compiler would otherwise generate, so the implicit super() in B() has nothing to call. The fix is an explicit super(...) with the arguments A needs. The same error appears for a class with no constructor at all, because its default constructor also calls super().
5Java 25: Statements Before this(...) or super(...)
Until recently, this(...) or super(...) had to be the very first statement of a constructor. That blocked harmless code, such as validating an argument before passing it up. Java 25 finalised flexible constructor bodies (JEP 513), which allow statements before the explicit constructor call, in a part called the prologue, as long as they do not use the instance being built.
Notice that the order of output is unchanged: the prologue only runs ordinary code that cannot see the half-built object, so the parent is still fully constructed before the child's body uses it. On Java 21 and earlier, this code does not compile, and the usual workaround was a static helper method called inside the super(...) argument list.
6Follow-up questions
Can a constructor call both this(...) and super(...)?
No. A constructor makes at most one explicit constructor call. If it chains with this(...), the constructor it calls is responsible for calling super, directly or through further this(...) calls. Otherwise the chain could initialise the parent twice.
What happens if two constructors call each other with this(...)?
The compiler rejects it with a recursive constructor invocation error, because the chain would never reach super and the object could never be built.
Would field initialisers in B run before or after A's constructors?
After. Instance field initialisers and instance initialiser blocks of B run right after the super(...) call returns and before the rest of B's constructor body. So A's constructors cannot see B's initialised fields, which is why calling an overridable method from A's constructor is risky.
7Conclusion
In this answer, we followed new B() and saw that it prints "A1 A B ": B() implicitly calls super(), A() chains to A(int) with this(1), A(int) implicitly calls Object(), and then the constructor bodies run on the way back down. A constructor makes exactly one constructor call, and if a superclass has no accessible no-arg constructor, the subclass must call super(...) explicitly. Classic Java required that call to be the first statement, while Java 25's flexible constructor bodies allow prologue statements that do not use the instance.
Open with
It prints A1, then A, then B, each followed by a space. The reason is that every constructor starts by calling another constructor. B() has no explicit call, so the compiler inserts super(), which goes to A().
Then, if they want more
A() starts with this(1), so before printing anything it chains to A(int). A(int) has its own implicit super(), which runs Object's constructor. Then the bodies run on the way back: A(int) prints A1, control returns to A(), which prints A, and finally back in B() it prints B.
So calls go up the chain first and bodies run on the way down, which guarantees the superclass part is fully built before subclass code runs. Two related rules: in classic Java, this(...) or super(...) has to be the first statement, and a constructor can make only one of those calls.
Java 25 finalised flexible constructor bodies, JEP 513, so now we can put statements before the call, like argument validation, as long as they don't touch this. And if A had only a constructor taking an int, B() wouldn't compile, because the implicit super() would have no no-arg constructor to call; I'd have to write super(someValue) explicitly.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does this print?": https://scrivio-interview-library.netlify.app/java/classes-and-objects/ac05b646f6/