I came across a problem while experimenting with subclasses and constractors and if possible i would like someone to explain it to me.
class AA {
AA(){System.out.println("AA");}
}
class BB {
BB() { System.out.println("BB");}
BB(int k) { System.out.println("BB"+k);}
}
class CC extends BB {
CC() { System.out.println("CC"); }
CC(int k) {
super(k);
for (int j=0;j<5;j++) {
System.out.print("o");
}
System.out.println(k);
}
}
class DD extends CC {
AA a3 = new AA();
DD(int k) { super(k);}
DD() {
this(2);
System.out.println("CC");
}
}
class Program {
public static void main(String[] a) {
DD d = new DD();
}
}
This prints
BB2
ooooo2
AA
CC
but i can't really undertstand why. After this(2) is called, shouldn't the program move forward to System.out.println("CC") and then create the instance of class AA? Seems like after it entered the DD() constractor, it executed half of it, then created a3 and then came back to continue the constractor execution.
(i was expecting:)
BB2
ooooo2
CC
AA
Thanks in advance for your help.