I am trying to do the exercise "reusing/E07_SimpleInheritance2" in "Thingking in Java 4th edition". the code works but the output in the console is:
A: New instance C
B: New instance B
C: New instance C
But I think C should be in front of B because the sentence "System.out.println('C: ' + str);" is in the C2's constructor, followed by instance B. Well, why not?
OK, I just realized when initialize, the sequences is: (Static variables, static fields) > (variables, fields) > Constructors. That's the reason. Problem solved, thanks for the guys bellow :)
class A2{
A2(String str){
System.out.println("A: " + str);
}
}
class B2{
B2(String str){
System.out.println("B: " + str);
}
}
class C2 extends A2{
C2(String str){
super(str);
System.out.println("C: " + str); //I think it should work first
}
B2 b = new B2("New instance B"); //Then followed by B
}
public class Q7_7_SimpleInheritance2 {
public static void main(String[] args) {
C2 c = new C2("New instance C");
}
}