The order in which constructors are called in Java

Source: Internet
Author: User
Tags export class

In the process of programming, we often encounter multiple classes of inheritance problems, then the constructors of multiple classes are called in what order?

Look at the code first:

1  Public classMeal {2      PublicMeal () {3System.out.println ("Meal constructor ()");4     }5 }6 7  Public classBread {8      PublicBread () {9System.out.println ("Bread Constructor ()");Ten     } One } A  -  Public classCheese { -      PublicCheese () { theSystem.out.println ("Cheese constructor ()"); -     } - } -  +  Public classLettuce { -      Publiclettuce () { +System.out.println ("Lettuce constructor ()"); A     } at } -  -  Public classLunchextendsMeal { -      PublicLunch () { -System.out.println ("Lunch Constructor ()"); -     } in } -  to  Public classPortablelunchextendsLunch { +      PublicPortablelunch () { -System.out.println ("Portablelunch Constructor ()"); the     } * } $ Panax Notoginseng  Public classSandwichextendsportablelunch{ -     PrivateBread bread=NewBread (); the     PrivateCheese Cheese =NewCheese (); +     PrivateLettuce lettuce=Newlettuce (); A      PublicSandwich () { theSystem.out.println ("Sandwich Constructor ()"); +     } -      $      Public Static voidMain (string[] args) { $         NewSandwich (); -     } -}

The preceding section of code shows the relationships between multiple classes:

Each class has its own initialization method.

The contents of the printout are as follows:

12 3 456   7

The constructor's invocation follows the following order:

(1): Call the base class constructor. This step is repeated recursively, first of all to construct the root of this hierarchy, then the next layer of the export class, and so on, until the lowest layer of the export class.

(2): The initialization method of the member is called in the order of Declaration.
(3): invokes the principal of the derived class constructor. (that is, the contents of the main method in the article)

The ultimate goal: to have all member variables in the method initialized.

The order in which the constructors are called is important. When inheriting, we already know everything about the base class and can access any member of the base class declared public and protected. This means that in an exported class, all members of the base class must be assumed to be valid. One standard approach is that once a construction action occurs, all members of the object are constructed. However, inside the constructor, we must make sure that the members we want to use are already built. The only way to ensure this is to call the base class constructor first . The members that are accessible to us in the base class are initialized when you enter the subclass constructor. In addition, knowing that all members in the constructor is valid is also because when member objects are defined within a class, they should be initialized whenever possible (that is, by combining the objects into a class).

If you follow this rule, you can guarantee that all base class members and the current object's member objects are initialized.

The order in which constructors are called in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.