hi
The
java compiler will interpret the code you have writen as below
class X {
Y b;
X(){
b = new Y();
System.out.print("X");
}
}
class Y {
Y(){
System.out.print("Y");
}
}
public class Z extends X {
Y y;
Z(){
y = new Y();
System.out.print("Z");
}
public static void main(
String[] args){
new Z();
}}
so the output will yxyz not onley with respect to object creation but even with respect to assignments say if we have code like
public class edf{
int i=10;
int j;
String str=new String();
edf(){}
.....
}
the compiler will consider the above code AS
public class edf{
int i;
int j;
String str;
edf(){
i=10;
str=new String();
}
.....
}
Observe carefully that the order we wrote the code that is i,j, and str are maintained same in the constructor but the code at class level will be moved in to constructor only when we have definition if only declared then compiler will not move it inside in this case see int j it is not moved inside the constructor
Thanks