public class X {
Y y = new Y();
static{
System.out.println("X static");
}
public X(){
System.out.println("X");
}
public static void main(String[] args) {
new Z();
}
}
public class Y {
public Y(){
System.out.println("Y");
}
}
public class Z extends X{
Y y = new Y();
static{
System.out.println("Z static");
}
public Z(){
System.out.println("Z");
}
}
运行结果
执行顺序:
- 基类static块
- 派生类static块
- 基类成员
- 基类构造器
- 派生类成员
- 派生类构造器