Java various Member initialization order

Source: Internet
Author: User

In other blogs have seen this about the initialization sequence of the article, think carefully, you really do not know exactly what the order is, and make a code test:

Class B extends A, then the type a is the parent class with static code blocks, common code blocks, static methods, static member variables, ordinary member variables, common methods.

 Public classloaderorderparent {Static{System.out.println ("Parent class--Static code block"); } {System.out.println ("Parent class--code block"); }     Public StaticString staticstringparent=initstaticstringinparent ();  PublicString stringinparent=initstringinparent ();  Publicloaderorderparent () {System.out.println ("Parent class--constructor method"); }     Public StaticString initstaticstringinparent () {System.out.println ("Parent class--Initialize static variables"); return"Initstaticstringinparent"; }     PublicString initstringinparent () {System.out.println ("Parent class--Initialize normal variables"); return"Initstringinparent"; }} Public classLoaderorderchildextendsloaderorderparent { PublicString stringinchild=Initstringinchild ();  Public StaticString staticstringinchild=Initstaticstringinchild (); {System.out.println ("Subclass-Method Block"); }    Static{System.out.println ("Subclass--static method block"); }     PublicLoaderorderchild () {System.out.println ("Subclass--Construction method"); }     Public StaticString Initstaticstringinchild () {System.out.println ("Subclass--Initialize static variables"); return"Staticstringinchild"; }     PublicString Initstringinchild () {System.out.println ("Subclass--Initialize ordinary variables"); return"Stringinchild"; }     Public Static voidMain (string[] args) {NewLoaderorderchild (); }}

In the subclass, execute the Main method, new Loaderorderchild ();

The output is:

Parent class--Static code block
Parent class--Initialize static variables
Subclass--Initialize static variables
Subclass--static method block
Parent class--code block
Parent class--Initialize normal variables
Parent class--construction method
Subclass--Initialize common variables
Subclass--Method block
Sub-class--construction method

Although the generic block of code for a subclass is written in front of a static code block, the static code base executes first

Java various Member 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.