所以上面的例子打印順序應該是這樣的:
parent static block 父類Static
child static block 子類static
parent block 父類缺省{}
parent constructor 父類構造函數
child block子類缺省{}
child constructor子類構造函數
class Parent{
static String name = “hello”;
static {
System.out.println(“parent static block”);
}
{
System.out.println(“parent block”);
}
public Parent(){
System.out.println(“parent constructor”);
}
}
class Child extends Parent{
static String childName = “hello”;
static {
System.out.println(“child static block”);
}
{
System.out.println(“child block”);
}
public Child(){
System.out.println(“child constructor”);
}
}
public class StaticIniBlockOrderTest {
public static void main(String[] args) {
new Child();//語句(*)
}
}