java函数加载顺序

子类A继承父类B  A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?


父类B静态代码块->子类A静态代码块->父类B非静态代码块->父类B构造函数->子类A非静态代码块->子类A构造函数


class SuperClass{
private static String STR = "Super Class Static Variable";
static{
System.out.println("Super Class Static Block:"+STR);
}

public SuperClass() {
System.out.println("Super Class Constructor Method");
}
{
System.out.println("Super Class Block");
}

}
public class ObjectInit extends SuperClass{
private static String STR = "Class Static Variable";
static{
System.out.println("Class Static Block:"+STR);
}

public ObjectInit() {
System.out.println("Constructor Method");
}
{
System.out.println("Class Block");
}
public static void main(String[] args) {
@SuppressWarnings("unused")
ObjectInit a =new ObjectInit();
}
}

Super Class Static Block:Super Class Static Variable
Class Static Block:Class Static Variable
Super Class Block
Super Class Constructor Method
Class Block
Constructor Method

你可能感兴趣的:(JAVA基础)