How non-static initializer block invoked? [closed]

自古美人都是妖i 提交于 2020-01-17 01:05:21

问题


public class A{

    {
        System.out.println("hi i am IIB");
    }

    public A(){
        System.out.println("hi i am constructor");
    }

    public static void main(String... args){
        A objA=new A();
    }

}

回答1:


iib will get execute before the constructor is called

No, this is not what happens. The code written in instance initializer blocks are a part of the constructor only at runtime. At compile time, the compiler moves the code of the instance initializer blocks to all the constructors of your class. So, at runtime, the constructor is effectively equivalent to:

public A() {
    super();
    System.out.println("hi i am IIB"); 
    System.out.println("hi i am constructor");
}

So, the statement in the instance initializer block is moved to the constructor before the statement already there.

Here's what the constructor looks like actually (Output of javap command):

 public A();
   Code:
      0: aload_0
      1: invokespecial #1    // Method java/lang/Object."<init>":()V
      4: getstatic     #2    // Field java/lang/System.out:Ljava/io/PrintStream;
      7: ldc           #3    // String hi i am IIB
      9: invokevirtual #4    // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     12: getstatic     #2    // Field java/lang/System.out:Ljava/io/PrintStream;
     15: ldc           #5    // String hi i am constructor
     17: invokevirtual #4    // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     20: return



回答2:


The Java Language Specification defines what is executed when a constructor is called:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

  2. If this constructor begins with an explicit constructor invocation (§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.




回答3:


No one will explicitly invoke the IIB, instead the IIB will be copied into every constructor (immediately after the implicit super() in your case). Consider adding a second constructor as follows,

public A(int arg){
    super();
    // Here is where the IIB will be copied.
    System.out.println("hi i am constructor 2");
}

public static void main(String... args) {
  A obj = new A();
  A objA = new A(2); // call the second constructor.
}

Which will output -

hi i am IIB
hi i am constructor
hi i am IIB
hi i am constructor 2


来源:https://stackoverflow.com/questions/20593606/how-non-static-initializer-block-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!