Java Constructors - Order of execution in an inheritance hierarchy

后端 未结 5 1221
予麋鹿
予麋鹿 2020-12-15 17:49

Consider the code below

  class Meal {
    Meal() { System.out.println(\"Meal()\"); }
  }

  class Bread {
    Bread() { System.out.println(\"Bread()\"); }
          


        
5条回答
  •  长情又很酷
    2020-12-15 18:23

    The first call in a constructor is always the super(...). This call is automatically inserted by the compiler if you don't write it down explicitly. No calls on the constructed object can happen before the call to super() returned. After super() finished, the fields are initialized in the order of appearance and then the rest of the constructor is executed.

提交回复
热议问题