Java Constructors - Order of execution in an inheritance hierarchy

后端 未结 5 1217
予麋鹿
予麋鹿 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:36

    I think there are two things going on here that are throwing you off. The first is that main is a static method, where as the member variables b, c, and l are non-static instance variables. That means that they belong to the objects of the class, not the class itself. So when the class is initialized to run the main method, the contructors of Bread, Cheese, and Lettuce are not called, since no instances of Sandwich have been created.

    Not until main actually run, and calls new Sandwich() are any objects actually constructed. At that point, the order of operations is:

    1. initialize member fields of the base class(es)
    2. run the base class(es) constructor(s)
    3. initialize member fields of this class
    4. run the constructor of this class

    This is done recursively, so in this case, the order would be

    1. init fields of Meal (none)
    2. run constructor of Meal (prints "Meal")
    3. init fields of Lunch (none)
    4. run constructor of Lunch (prints "Lunch")
    5. init fields of PortableLunch (none)
    6. run constructor of PortableLunch (prints "PortableLunch")
    7. init fields of Sandwich (prints "Bread", "Cheese", and "Lettuce")
    8. run constructor of Sandwich (prints "Sandwich")

    The purpose of this order is to ensure that the base class is fully initialized before any code in the subclass is run. This is needed because the within the constructor of the sub-class, it may call a method on the base class. Bad things would happen if that base class did not initialize its members first.

提交回复
热议问题