JAVA initialization blocks

后端 未结 6 1746
谎友^
谎友^ 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:50

    The code is converted by compiler to something like this:

    public class Main extends Foo {
      void _init()
      {System.out.println("Main init");}
    
      public Main()
      {
        super();
        _init();
        {System.out.println("Main constr");}
      }
    
    }
    

    The main rules are:

    • super() is called before any initialization code of the current class
    • initializer blocks are called before the body of the constructor

提交回复
热议问题