Java Initialization Order

Source: Internet
Author: User

Single class

The order of initialization is as follows: (1) static data members, (2) static data blocks, (3) instance data members, (4) instance data blocks, (5) constructors.

The first round, scanning static things to initialize, contains (1), (2).

The second round, scanning non-static things to Initialize, contains (3).

In the third round, the constructor is initialized, containing (4).

Have inherited

(1) The parent class static data member, (2) The parent class static data block, (3) subclass static data member, (4) subclass static data block, (5) Parent class instance data member, (6) Parent class instance data block, (7) Parent class constructor, (8) Child class instance data member, (9) Subclass instance data block, (10) subclass constructor.

In the first round, the scanning of static things is initialized, whether it is a subclass or a parent class, containing (1), (2), (3), (4).

In the second round, scans the parent class for non-static initialization, which is limited to the parent class, which contains (5), (6).

Third round, initializes the parent class constructor, which contains (7).

The fourth round, the scan subclass non-static things to initialize, attention is limited to subclasses, including (8), (9).

In the fifth round, the sub-class constructor is initialized, containing (10).

Java Initialization Order

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.