Java虚拟机在载入类的时候。先初始化父类静态变量。再初始化子类静态变量。然后载入父类,最后载入子类
public class Parent {
static{
System.out.println("static parent");
}
public Parent(){
System.out.println("parent loaded");
}
public void getaaa(){
System.out.println("parent aaa");
}
}
public class Child extends Parent{
public Child(){
System.out.println("child load");
}
public void getaaa(){
System.out.println("child aaa");
}
static{
System.out.println("static child");
}
public static void main(String[] args){
Child child = new Child();
child.getaaa();
}
}
结果 :
static parent
static child
parent loaded
child load
child aaa