首页 > 代码库 > Java虚拟机类加载顺序

Java虚拟机类加载顺序

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