Javaにおけるstaticコードブロック,コードブロック,mainメソッド,構築メソッドの実行順序


サブクラス:
public class TestPriority extends TestFatherPriority {
    public TestPriority() {
        System.out.println("      ");
    }

    static {
        System.out.println("       ");
    }

    {
        System.out.println("     ");
    }

    public static void main(String[] args) {
        System.out.println("before  main");
        new TestPriority();
        System.out.println("after  main");
    }
}

親:
public class TestFatherPriority {
    public TestFatherPriority() {
        System.out.println("      ");
    }

    static {
        System.out.println("       ");
    }

    {
        System.out.println("     ");
    }

    public static void main(String[] args) {
        System.out.println("before  main");
        new TestFatherPriority();
        System.out.println("after  main");
    }
}


サブクラスmainメソッドを実行し、出力:
       
       
before  main
     
      
     
      
after  main

結論を出す:
①継承がない場合の実行手順:
静的コードブロック->mainメソッド->コードブロック->構築方法
②継承がある場合の実行手順:
親静的コードブロック->子静的コードブロック->mainメソッド->親コードブロック->親構造メソッド->子コードブロック->子構造メソッド