如以下面试题:
/**
* @author hjs
* @date 2019/3/62:41
* @Dec
*/
public class B {
public static B t1 = new B();
public static B t2 = new B();
{
System.out.println("父类构造块");
}
static {
System.out.println("父类静态块");
}
}
/**
* @author hjs
* @date 2019/3/62:35
* @Dec
*/
public class A extends B {
public static A t1 = new A();
public static A t2 = new A();
{
System.out.println("构造块");
}
static {
System.out.println("静态块");
}
public static void main(String[] args) {
A t = new A();
}
}
输出结果:
父类构造块、父类构造块、父类静态块、父类构造块、构造块、父类构造块、构造块、静态块、父类构造块、构造块 B1初始化的:父类构造块;
B2初始化的:父类构造块、父类静态块
A1初始化的:父类构造块、构造块
A2初始化的:父类构造块、构造块、静态块
main函数创建A时打印的:父类构造块、构造块
结论:父类静态属性、父类静态代码块、子类静态属性、子类静态代码块、代码块