Java class initialization order, java class initialization order

Source: Internet
Author: User

Java class initialization order, java class initialization order

In java, when we create an object, there is a certain order of loading the members, initialization blocks, and constructor of the object. See the following figure:

I. initialization sequence of a single class (without a base class:

Public class Parent {

Static String staticStr = "parent static variable ";

Private String str = "parent class member variable ";

 

Static {

System. out. println (staticStr );

System. out. println ("parent class static initialization block ");

}

 

{

System. out. println (str );

System. out. println ("parent class initialization block ");

}

 

Public Parent (){

System. out. println ("parent class structure ");

}

 

Public static void main (String [] args ){

New Parent ();

// New Parent ();

}

}

Output: static variables of the parent class

Static initialization block of the parent class

Parent class member variable

Parent class initialization Block

Parent Class Construction

/* If new Parent () is released on the second line, the following content will be added to the output:

Parent class member variable

Parent class initialization Block

Parent Class Construction

*/

Single class sequence: ① static (Member, block)> non-static (Member, block)> Construction

② The Order of members and blocks depends on "The front and back positions of the members and blocks they define in the class, and who is first initialized ".

③ Static members or blocks belong to the class. Therefore, they are initialized first and only once.

④ The structure is finally executed.

 

Ii. initialization sequence of inheritance classes

 

Class SubClass extends Parent {

Static String staticStr = "subclass static variables ";

Private String str = "subclass member variable ";

 

Static {

System. out. println (staticStr );

System. out. println ("subclass static initialization block ");

}

 

{

System. out. println (str );

System. out. println ("subclass initialization block ");

}

 

Public SubClass (){

System. out. println ("subclass structure ");

}

 

Public static void main (String [] args ){

System. out. println ("subclass main method ");

New SubClass ();

}

}

Output: static variables of the parent class

Static initialization block of the parent class

Static subclass Variables

Static initialization block of subclass

Subclass main method

Parent class member variable

Parent class initialization Block

Parent Class Construction

Subclass member variable

Subclass initialization Block

Subclass Construction

Inheritance class sequence:

① Static parent class (Member, block)> static subclass (Member, block)> non-static parent class (Member, block)> parent class structure> non-static subclass (member, block)> subclass Construction

② Similarly, the order of members and blocks is the same as that in a single class.

③ A child has a parent, but the static (member and block) of the Child class is prior to the non-static (member and block) of the parent class, because the static class belongs to the class. For non-static and constructor, naturally, the parent class is fully loaded and then the Child class is loaded.

 

Follow ginger to talk about technology, no.: helojava, or scan the following QR code.


One post per day, chicken soup for technical purposes.

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.