Java 类、变量加载顺序

1 结论

1.1 没有父类的情况

  1. 静态变量/代码块(优先级一样,看谁在前面)
  2. 实例变量/代码块(优先级一样,看谁在前面)
  3. 构造方法

1.2 有父类的情况

  1. 父类静态变量/代码块(优先级一样,看谁在前面)
  2. 子类静态变量/代码块(优先级一样,看谁在前面)
  3. 父类实例变量/代码块(优先级一样,看谁在前面)
  4. 父类构造方法
  5. 子类实例变量/代码块(优先级一样,看谁在前面)
  6. 子类构造方法

1.3 内部类加载

内部类也是类,仅在使用的时候加载。

2 验证

2.1 没有父类的情况

public class Parent {
  private static int staticVar1 = staticPrint("var", 1);

  static {
    staticPrint("code", 1);
  }

  private static int staticVar2 = staticPrint("var", 2);

  static {
    staticPrint("code", 2);
  }

  private int var1 = print("var", 1);

  {
    print("code", 1);
  }

  private int var2 = print("var", 2);

  {
    print("code", 2);
  }

  public Parent() {
    System.out.println("--->constructor--->" + Parent.class.getSimpleName());
  }

  private class InnnerA {
    public InnnerA() {
      System.out.println("----innerA");
    }
  }

  private static class InnnerB {
    public InnnerB() {
      System.out.println("----innerB");
    }
  }

  private static int staticVar3 = staticPrint("var", 3);

  static {
    staticPrint("code", 3);
  }

  private static int staticVar4 = staticPrint("var", 4);

  static {
    staticPrint("code", 4);
  }

  private int var3 = print("var", 3);

  {
    print("code", 3);
  }

  private int var4 = print("var", 4);

  {
    print("code", 4);
  }


  private static int staticPrint(String type, int value) {
    System.out.println("--->static " + type + " " + value + "--->" + Parent.class.getSimpleName());
    return 0;
  }

  private int print(String type, int value) {
    System.out.println("--->" + type + " " + value + "--->" + Parent.class.getSimpleName());
    return 0;
  }

  private void innerA() {
    InnnerA innnerA = new InnnerA();
  }

  private static void innerB() {
    InnnerB innnerB = new InnnerB();
  }

}

;对于上面的类,执行以下代码

Parent parent = new Parent();

控制台输出

--->static var 1--->Parent
--->static code 1--->Parent
--->static var 2--->Parent
--->static code 2--->Parent
--->static var 3--->Parent
--->static code 3--->Parent
--->static var 4--->Parent
--->static code 4--->Parent
--->var 1--->Parent
--->code 1--->Parent
--->var 2--->Parent
--->code 2--->Parent
--->var 3--->Parent
--->code 3--->Parent
--->var 4--->Parent
--->code 4--->Parent
--->constructor--->Parent

执行

    Parent parent = new Parent();
    parent.innerA();

控制台输出

--->static var 1--->Parent
--->static code 1--->Parent
--->static var 2--->Parent
--->static code 2--->Parent
--->static var 3--->Parent
--->static code 3--->Parent
--->static var 4--->Parent
--->static code 4--->Parent
--->var 1--->Parent
--->code 1--->Parent
--->var 2--->Parent
--->code 2--->Parent
--->var 3--->Parent
--->code 3--->Parent
--->var 4--->Parent
--->code 4--->Parent
--->constructor--->Parent
----innerA

2.2 有父类的情况

public class Child extends Parent {
  private static int staticVar1 = staticPrint("var", 1);

  static {
    staticPrint("code", 1);
  }

  private static int staticVar2 = staticPrint("var", 2);

  static {
    staticPrint("code", 2);
  }

  private int var1 = print("var", 1);

  {
    print("code", 1);
  }

  private int var2 = print("var", 2);

  {
    print("code", 2);
  }

  public Child() {
    System.out.println("--->constructor--->" + Child.class.getSimpleName());
  }

  private static int staticVar3 = staticPrint("var", 3);

  static {
    staticPrint("code", 3);
  }

  private static int staticVar4 = staticPrint("var", 4);

  static {
    staticPrint("code", 4);
  }

  private int var3 = print("var", 3);

  {
    print("code", 3);
  }

  private int var4 = print("var", 4);

  {
    print("code", 4);
  }


  private static int staticPrint(String type, int value) {
    System.out.println("--->static " + type + " " + value + "--->" + Child.class.getSimpleName());
    return 0;
  }

  private int print(String type, int value) {
    System.out.println("--->" + type + " " + value + "--->" + Child.class.getSimpleName());
    return 0;
  }
}

执行

    Child child = new Child();

控制台输出

--->static var 1--->Parent
--->static code 1--->Parent
--->static var 2--->Parent
--->static code 2--->Parent
--->static var 3--->Parent
--->static code 3--->Parent
--->static var 4--->Parent
--->static code 4--->Parent
--->static var 1--->Child
--->static code 1--->Child
--->static var 2--->Child
--->static code 2--->Child
--->static var 3--->Child
--->static code 3--->Child
--->static var 4--->Child
--->static code 4--->Child
--->var 1--->Parent
--->code 1--->Parent
--->var 2--->Parent
--->code 2--->Parent
--->var 3--->Parent
--->code 3--->Parent
--->var 4--->Parent
--->code 4--->Parent
--->constructor--->Parent
--->var 1--->Child
--->code 1--->Child
--->var 2--->Child
--->code 2--->Child
--->var 3--->Child
--->code 3--->Child
--->var 4--->Child
--->code 4--->Child
--->constructor--->Child

2.3 测试源码

https://github.com/niuhp/basic-java/tree/master/src/main/java/com/niuhp/basic/loading