Simple verification of java object initialization sequence

Source: Internet
Author: User

The following applet verifies the call sequence of the parent class constructor, member variable initialization function, and non-static initialization block when calling the object constructor, regardless of static members and static initialization blocks.

Derive Member m1 = Member ("Member 1" "Initial Block ()" "Derive ()" Member m2 = Member ("Member 2" I = "getInt () "2" Base () "" Member () "+

 

The following conclusions are drawn: without considering static member initialization, when calling an object constructor, the program first calls the constructor of the parent class (you can use the super keyword to specify the constructor of the parent class, otherwise, the constructor without parameters is called by default and must be called in the first line of the constructor of the subclass ), the initialization functions and static initialization blocks of static member variables are executed in the code sequence. If the member variables do not specify a value, the default value is assigned, that is, the basic data type is 0 or false, and the object is null. Finally, the self-constructor is called.

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.