I guess it's pretty obvious when you make it explicit:
class A {
A() {
System.out.println("3");
}
}
class B extends A {
B(String foo) {
super();
System.out.println("2" + foo);
}
}
class C extends B {
C(String foo) {
super(foo);
System.out.println("1");
}
}
When using these explicit constructor calls, you can see that super(...)
is always the first instruction. Though this is what happens:
new C("foo");
will call the C
constructor
- first line in
C
constructor will call super constructor (B
)
B
constructor is called
- first line in
B
constructor will call super constructor (A
)
A
constructor is called (prints 3
)
- when all lines of
A
constructor are done, it will return control to B
constructor
B
constructor will execute all lines after the super
call (prints 2
+ foo
)
- then it will return control to
C
constructor
C
constructor will execute all lines after the super
call (prints 1
)
When you don't create an explicit constructor this code will still be run in exactly the same order.
It's basically the same as calling methods from within methods, or calling super methods. The only difference is, that you must call the super constructor before you execute any other code.