9.4 super, this & Constructor Execution Order
Key Takeaways
Use super.member to access hidden superclass fields or call overridden superclass instance methods, but never inside a static context and never chained as super.super
An explicit this(...) or super(...) call must be the very first statement in a constructor, and a single constructor may contain at most one such call
A normal subclass constructor without an explicit this or super invocation begins with an implicit
super().If no accessible superclass constructor is applicable to
super(), explicitly select a compatible one or use athis(...)chain that does.Class initialization is separate from loading. During construction, each class’s instance initializers run once, after its superclass constructor completes and before its remaining constructor body.
super, this & Constructor Execution Order
When subclasses hide fields, override methods, or add overloaded constructors, Java relies on this and super to disambiguate members and chain initialization cleanly from java.lang.Object down to the concrete subclass.
super.member vs. this.member
Inside an instance method or constructor:
this.memberaccesses a member through the current instance and can disambiguate a field from a parameter. A method invocation throughthiscan still dispatch to a subclass override.super.memberbegins lookup in the superclass. A call such assuper.bonus()invokes the selected superclass implementation without virtual dispatch to the current class.
class Employee {
protected double base = 50000;
public double bonus() { return 1000; }
}
class Manager extends Employee {
private double base = 80000; // Hides Employee.base
@Override
public double bonus() {
return super.bonus() + 2500; // Calls Employee.bonus() -> 3500
}
public void report(double base) {
System.out.println(base); // Local parameter
System.out.println(this.base); // Manager.base (80000)
System.out.println(super.base); // Employee.base (50000)
}
}
Hard Restrictions on super and this
- No
staticcontext: Neitherthisnorsupercan appear inside astaticmethod,staticinitializer block, orstaticfield initializer (non-static variable super cannot be referenced from a static context). - No
super.superchaining: Writingsuper.super.bonus()to skipParentand reachGrandparentis a compile-time error.superis a keyword qualifier, not an object reference expression. - Cannot reach
privateparent members: Private members are not inherited. A separate top-level subclass cannot usesuper.secretto bypass private access.
Constructor Chaining With this(...) and super(...)
For ordinary Java 8 subclasses, each constructor begins by delegating to another constructor in the same class or to a superclass constructor, either explicitly or with an implicit super(). Four strict rules govern this syntax:
| Rule | Requirement | Result if Violated |
|---|---|---|
| 1. First statement only | this(...) or super(...) must be the very first statement in the constructor | Compile-time error (call to super/this must be first statement) |
| 2. At most one call | A constructor cannot contain both this(...) and super(...), nor two this(...) calls | Compile-time error |
3. Implicit super() | If neither this(...) nor super(...) is written, javac inserts super(); automatically | Invokes an accessible superclass constructor applicable to zero arguments, which may be varargs |
| 4. No cycles or method calls | Constructors only; recursive this(...) cycles are forbidden | Compile-time error (recursive constructor invocation) |
Look at how these rules interact in code:
class Device {
Device(String serial) { }
}
class Sensor extends Device {
private int rate;
Sensor() {
this("DEFAULT", 10); // Valid: chains to overloaded Sensor constructor
// super("X"); // DOES NOT COMPILE: cannot have both this() and super()
}
Sensor(String serial, int rate) {
// System.out.println(rate); // DOES NOT COMPILE if placed BEFORE super(serial)!
super(serial); // Must be the VERY FIRST statement
this.rate = rate;
}
}
Notice that Sensor() does not call super(...) directly—it delegates to Sensor(String, int) via this("DEFAULT", 10), and that constructor calls super(serial). Eventually, every this(...) chain must terminate at a constructor that invokes super(...).
Warning
The explicit constructor invocation cannot use the current object’s instance members. Arguments can use constructor parameters, static members, or another object’s accessible members.
The Missing No-Arg Parent Constructor Trap
The compiler synthesizes a default no-argument constructor only when a class declares zero constructors. A parent that declares only Parent(int) has no constructor applicable to super(). In the examples below, the subclass must explicitly select the available constructor:
class Parent {
Parent(int x) {
System.out.println("Parent: " + x);
}
}
// Example A: Compiler inserts default constructor Child() { super(); }
// class ChildA extends Parent { } // DOES NOT COMPILE: Parent() does not exist!
// Example B: Explicit constructor without explicit super(...) or this(...)
// class ChildB extends Parent {
// ChildB() { } // Implicit super() does not compile here.
// }
// Example C: Fixed by calling super(int)
class ChildC extends Parent {
ChildC() {
super(100); // Compiles cleanly!
}
}
This trap is one of the highest-frequency questions in inheritance sections: even an empty class ChildA extends Parent {} fails to compile if Parent defines Parent(int x) and no Parent().
Tracing Initialization & Constructor Order Across Levels
For this ordinary class hierarchy, assume the classes have not yet been initialized and that each initializer and constructor completes normally:
- Class initialization: On an active use that triggers initialization, ordinary static field initializers and static blocks run from
GrandparenttoParenttoChild, in textual order within each class. Loading alone does not trigger initialization, and constant variables have special rules. When relevant, class initialization also initializes its superinterfaces that declare default methods. This example has no such interfaces. - Constructor chaining (bottom-up):
new Child()invokesChild()->super()(Parent()) ->super()(Grandparent()) ->super()(Object()). - Instance initialization & constructor bodies (top-down):
Object()completes.Grandparentinstance fields &{ ... }initializer blocks run in textual order ->Grandparentconstructor body completes.Parentinstance fields &{ ... }initializer blocks run in textual order ->Parentconstructor body completes.Childinstance fields &{ ... }initializer blocks run in textual order ->Childconstructor body completes.
Trace the exact output of this complete Java SE 8 program:
class Grandparent {
static { System.out.print("1 "); }
{ System.out.print("4 "); }
Grandparent() { System.out.print("5 "); }
}
class Parent extends Grandparent {
static { System.out.print("2 "); }
{ System.out.print("6 "); }
Parent(String msg) {
// Implicit super() runs Grandparent instance init + constructor first
System.out.print("7:" + msg + " ");
}
}
public class Child extends Parent {
static { System.out.print("3 "); }
{ System.out.print("8 "); }
Child() {
this(9);
System.out.print("10 ");
}
Child(int n) {
super("OK");
System.out.print(n + " ");
}
public static void main(String[] args) {
new Child();
}
}
Exact output: 1 2 3 4 5 6 7:OK 8 9 10
Let us verify every token:
- Class initialization before the launcher invokes main:
Grandparent(1),Parent(2),Child(3). new Child()chains:Child()->this(9)->super("OK")(Parent("OK")) -> implicitsuper()(Grandparent()) ->Object().Grandparentfinishes: instance block (4), constructor body (5).Parentfinishes: instance block (6), constructor body (7:OK).Childfinishes: instance block (8runs once aftersuper("OK")returns, NOT once perthis(...)call!),Child(int)body (9), thenChild()body (10).
Important
Instance initializer blocks and instance field initializers run immediately after super(...) completes—not after this(...)! Even when Child() chains through this(9), Child's instance block (8) runs exactly once before Child(int) executes its body.
Given:
class Alpha {
Alpha(String s) { System.out.print(s); }
}
class Beta extends Alpha {
Beta() { System.out.print("B"); }
public static void main(String[] args) { new Beta(); }
}
What is the result?
Prints B
Prints nullB
Throws RuntimeException because Alpha was not initialized
Fails to compile because Alpha has no no-argument constructor
Which constructor declaration inside class Sub extends Super compiles if Super() and Super(int) are accessible and neither declares checked exceptions?
Sub() { System.out.println("start"); super(5); }
Sub() { this(5); } Sub(int x) { super(x); }
Sub() { super(); this(5); } Sub(int x) { }
Sub() { this(5); } Sub(int x) { this(); }
What does the following program print?
class One {
static { System.out.print("A"); }
{ System.out.print("B"); }
One() { System.out.print("C"); }
}
class Two extends One {
static { System.out.print("D"); }
{ System.out.print("E"); }
Two() { this(1); System.out.print("F"); }
Two(int x) { System.out.print("G"); }
public static void main(String[] args) { new Two(); }
}
ABCDEFG
ADBCGEF
ADBCEGF
BCADEGF
Sections you finish are checked off in the contents.