什么是初始化块,有什么作用?静态初始化块与非静态初始化快有什么区别?
静态初始化块、构造函数、非静态初始化块在一个类中的执行顺序?
下面这个程序输出的结果是什么?为什么是这个结果?
public class TestK{
public static void main(String[] args){
new A();
}
{
System.out.println("Test' non-static initialization"+"blook is invoked");
}
static{
System.out.println("Test' static initialization"+"blook is invoked");
}
}
class A extends B{
A(){
System.out.println("A's constructor is invoked");
}
{
System.out.println("A's non-static initialization"+"blook is invoked");
}
static{
System.out.println("A's static initialization"+"blook is invoked");
}
}
class B{
B(){
System.out.println("B's constructor is invoked");
}
{
System.out.println("B's non-static initialization"+"blook is invoked");
}
static{
System.out.println("B's static initialization"+"blook is invoked");
}
}