JAVA initialization blocks

后端 未结 6 1752
谎友^
谎友^ 2021-01-21 18:03

As some sources say, the Java instance initialization blocks are executed whenever instance is created or right before constructor. But imagine this case:

public         


        
6条回答
  •  借酒劲吻你
    2021-01-21 18:28

            new Main(); 
    

    the above state ment will first goto Main(). But before execution of anything in Main(), super() is called. So, Foo() is called. Now, in Foo() a check is made to see instance initialization block. So, you get. "Foo init" . Next the statements in Foo() are executed. So, you get- "Foo constr". Next the control is returned to Main() which now checks whether an initialization block exists for Main class. So, "Main Init" is printed.. then other statements of Main() are printed. The definition is right.. The understanding.. well... depends on how you see it...

提交回复
热议问题