Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

What does this print, and why?

Java
class Base {
Base() {
show();
}
void show() {
System.out.println("base");
}
}
class Child extends Base {
private String name = "child";
private final int n = 5;
private final Integer boxed = 7;
@Override void show() {
System.out.println(name + " " + n + " " + boxed);
}
}
new Child();

It prints null 5 null. The Base constructor runs before Child's field initializers, and it calls show(), which dispatches to Child's override on an object whose fields still hold default values, so name and boxed are null. n prints 5 because a final int initialized with a constant expression is a compile-time constant, and the compiler inlines the value 5 into show(). The lesson is never to call overridable methods from a constructor.

Background to this question

Object construction in Java happens in a strict order, and most of the time we never need to think about it. The moment a superclass constructor calls a method that a subclass overrides, that order becomes visible, and the result looks impossible: fields that are clearly initialized print as null. The same mistake causes NullPointerExceptions in real frameworks and in template-method style base classes. In this answer, we will recap how an object is created, trace this exact program step by step, explain why n behaves differently from boxed, and see how to avoid the trap.

1A Quick Recap of How new Child() Builds an Object

When we write new Child(), the JVM first allocates memory for the whole object, including the fields of Base and Child, and sets every field to its default value: null for references, 0 for numbers, false for booleans. Only then do constructors run.

Every constructor starts by calling its superclass constructor, either explicitly with super(...) or through an implicit super() the compiler inserts. After the superclass constructor returns, the class runs its own instance field initializers and instance initializer blocks, in the order they appear in the source, and then the rest of its constructor body. So for Child, the order is: Child() calls Base(), Base() calls Object(), Base's initializers and body run, and only after Base() returns do Child's field initializers run.

Method calls on an object use dynamic dispatch: the JVM looks at the runtime class of the object, not the class where the call is written. Inside Base's constructor, this is already a Child object, so calling show() runs Child.show().

Step throughThe order of events in new Child()
CallerChild()Base()Child.show()
new Child(): memory allocated, fields null / 0
implicit super()
show(): dynamic dispatch to the override
prints name + n + boxed -> null 5 null
returns
Base() done
now run name = "child", boxed = 7
object ready

8 messages in order. Play them, or step through them one at a time.

Child's field initializers only run after Base() has returned.

2Tracing the Program

Here is the code again with the state of the fields at the moment show() runs.

Java
class Base {
Base() { show(); } // runs before Child's initializers
void show() { System.out.println("base"); }
}
class Child extends Base {
private String name = "child"; // not yet assigned when show() runs
private final int n = 5; // compile-time constant
private final Integer boxed = 7; // not a constant: Integer.valueOf(7) at runtime
@Override void show() { System.out.println(name + " " + n + " " + boxed); }
}
new Child(); // null 5 null

When Base() calls show(), the call goes to Child.show() because the object is a Child. At that moment name and boxed still have their default value null, because the code that assigns "child" and 7 belongs to Child's constructor and has not run yet. So the string concatenation produces null for both. After Base() returns, Child's initializers do assign name and boxed, so any later call to show() would print child 5 7. The bug is only visible during construction, which is what makes it hard to spot.

3Why n Prints 5

n is also a field of Child, so why is it not 0? The answer is that n is a constant variable. The language specification defines a constant variable as a final variable of a primitive type or String that is initialized with a constant expression, such as the literal 5. The compiler replaces every use of such a variable with its value. Inside show(), the expression name + " " + n is compiled as if we had written name + " " + 5, and it never reads the field at all. We can see this with javap -c: show() loads the constant 5 directly.

boxed looks similar, but Integer is not a primitive type or String, so final Integer boxed = 7 is not a constant variable. It compiles to a field assignment from Integer.valueOf(7) in Child's constructor, and show() reads the field, which is still null. If we changed n to private final int n = Integer.parseInt("5"), it would no longer be a constant expression, and the output would become null 0 null.

Why each field printed what it did
FieldConstant variable?Value seen inside Base()
String name = "child"no, not finalnull (default)
final int n = 5yes: final primitive, constant expression5, inlined by the compiler
final Integer boxed = 7no: Integer is not primitive or Stringnull (default)
final int n = Integer.parseInt("5")no: not a constant expression0 (default)

4How to Avoid the Trap

The rule is simple: a constructor should never call a method that a subclass can override. While a superclass constructor runs, the subclass part of the object is not ready, so any override sees a half-built object. Effective Java gives the same advice, and the javadoc of many JDK classes documents which methods their constructors call for the same reason.

There are three common fixes. Make the method private or final, so it cannot be overridden and always runs the base version. Move the work out of the constructor into a static factory method that first constructs the object and then calls the method, when the subclass is fully initialized. Or pass the needed values into the superclass constructor as parameters, instead of asking the subclass for them through an overridden method. Since Java 21, the compiler can help: javac -Xlint:this-escape warns when a constructor lets this escape to code that a subclass could override.

5Follow-up questions

What is the full initialization order with static blocks?

Static initialization happens once per class, when the class is first initialized, and superclass first: Base's static fields and static blocks in source order, then Child's. Then, for each new Child(): memory is allocated with default values, Child() calls Base(), Base() calls Object(), then Base's instance field initializers and instance blocks run in source order, then the rest of Base's constructor body, then Child's instance initializers and blocks in source order, and finally the rest of Child's constructor body. So statics are parent then child once, and instance setup is parent fully, then child fully, for every object.

Would the result change if show() were called after construction?

Yes. Once new Child() has returned, all initializers have run, so calling show() prints child 5 7. The problem only exists for calls made while a superclass constructor is still running.

Would making name final fix it?

Only if its initializer is a constant expression. final String name = "child" is a constant variable, so it would be inlined and print child. But final String name = computeName() would still print null, so relying on that is fragile; the real fix is not calling the override from the constructor.

6Conclusion

In this answer, we saw that new Child() runs the Base constructor before Child's field initializers, so when Base() calls the overridden show(), it runs on a Child whose fields still hold default values, and name and boxed print as null. n prints 5 because a final int with a constant initializer is a constant variable that the compiler inlines into show(). The lesson is to never call overridable methods from constructors: make them private or final, use a factory method, or pass values to the superclass constructor.

What to say in the interviewAbout 80 seconds aloud

Open with

It prints null 5 null. When we call new Child(), memory is allocated and every field starts at its default, null or zero. Child's constructor first calls Base's constructor, and only after that returns do Child's field initializers run.

Then, if they want more

Base's constructor calls show(), and because of dynamic dispatch that runs Child's override, on an object whose fields have not been assigned yet.

So name is null and boxed is null. n is the odd one: it is a final int initialized with the literal 5, which makes it a compile-time constant, a constant variable, so the compiler replaces n with 5 inside show() and never reads the field. boxed is final too, but Integer is not a primitive or String, so it is a normal runtime assignment and still null at that point.

If n were initialized from a method call, it would print 0. The lesson is never to call overridable methods from a constructor, because the subclass sees a half-built object. I would make such methods private or final, move the call into a static factory method after construction, or pass the values into the superclass constructor. On Java 21 and later, javac -Xlint:this-escape can warn about this pattern.

About this answer

Last updated
12 November 2026
Versions it names
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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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, and why?": https://scrivio-interview-library.netlify.app/java/coding-problems/ab52614bac/