public class TestLab {
static Test aStatic=new Test();
public static void main(String[] args) {
TestLab obj=new TestLab();
}
static{
System.out.println("In static block of TestLab");
}
}
public class Test {
static Test ref=new Test();
Test()
{
System.out.println("Default Constructor of Test");
}
static
{
System.out.println("In Static Block of Test");
}
{
System.out.println("In instance block of Test");
}
}
通常、静的ブロックはクラスのロード中に最初に実行されます。上記の例を実行すると、次の出力が表示されます。
Test のインスタンスブロック内
テストのデフォルト コンストラクタ
テストの静的ブロック内
Test のインスタンスブロック内
テストのデフォルト コンストラクタ
TestLab の static ブロック内
テストクラスのインスタンスブロックとデフォルトコンストラクタがテストクラスの静的ブロックの前に実行されるのはなぜですか?