为什么在构造函数之前执行实例初始化块

时间:2015-06-28 14:49:03

标签: java constructor initialization

我知道静态块是在加载类时初始化的,因为类只在程序中加载一次,所以只初始化一次。

IIB(实例初始化块)在每次创建类的实例时被初始化,而构造函数则相同:它们在对象创建期间执行。

我不明白为什么下面的程序IIB在构造函数之前执行。 代码 -

public class Hello {

    public static void main(String args[]) {
        C obj = new C();
    }
}

class A {

    static {
        System.out.println("Inside static block of A");
    }

    {
        System.out.println("Inside IIB of A");
    }

    A() {
        System.out.println("Inside NO-ARGS constructor of A");
    }
}

class B extends A {

    static {
        System.out.println("Inside static block of B");
    }

    {
        System.out.println("Inside IIB of B");
    }

    B() {
        System.out.println("Inside NO-ARGS constructor of B");
    }
}
class C extends B {

    static {
        System.out.println("Inside static block of C");
    }

    {
        System.out.println("Inside IIB of C");
    }

    C() {
        System.out.println("Inside NO-ARGS constructor of C");
    }
}

与构造函数相比,为什么首先执行IIB?

1 个答案:

答案 0 :(得分:6)

Java编译器注入初始化块at the beginning of your constructors(在调用超构造函数之后)。为了让您更好地理解,我已编译了以下课程

public class Foo extends SuperFoo {
    private String foo1 = "hello";
    private String foo2;
    private String foo3;
    {
        foo2 = "world";
    }
    public Foo() {
        foo3 = "!!!";
    }
}

并通过 javap 反编译器运行它:

Compiled from "Foo.java"
public class Foo extends SuperFoo {
  private java.lang.String foo1;
  private java.lang.String foo2;
  private java.lang.String foo3;
  public Foo();
    Code:
       0: aload_0
       1: invokespecial #12                 // Method SuperFoo."<init>":()V
       4: aload_0
       5: ldc           #14                 // String hello
       7: putfield      #16                 // Field foo1:Ljava/lang/String;
      10: aload_0
      11: ldc           #18                 // String world
      13: putfield      #20                 // Field foo2:Ljava/lang/String;
      16: aload_0
      17: ldc           #22                 // String !!!
      19: putfield      #24                 // Field foo3:Ljava/lang/String;
      22: return
}
相关问题