public class Test1 {
static{
System.out.println("执行静态初始化块test1...");
}
{
System.out.println("执行初始化块test1");
}
public Test1(){
System.out.println("执行test1构造函数");
}
}
public class Test2 extends Test1{
static{
System.out.println("执行静态初始化块test2...");
}
{
System.out.println("执行初始化块test2");
}
public Test2(){
System.out.println("执行test2构造函数");
}
}
public class Test3 extends Test2{
static{
System.out.println("执行静态初始化块test3...");
}
{
System.out.println("执行初始化块test3");
}
public Test3(){
System.out.println("执行test3构造函数");
}
}
----------------------------------------------------------------------------------------------------
public class Test {
public static void main(String[] args){
Test3 t = new Test3();
}
}
结果:
执行静态初始化块test1...
执行静态初始化块test2...
执行静态初始化块test3...
执行初始化块test1
执行test1构造函数
执行初始化块test2
执行test2构造函数
执行初始化块test3
执行test3构造函数