The execution order of static code blocks, non-static code blocks, and construction methods between Java child parent classes

Source: Internet
Author: User

Subclass a inherits the parent class B,a a=new a ();

The correct order of execution is: parent class B Static code block, subclass a static code block, parent class B non-static code block, parent Class B constructor, subclass a non-static code block, sub-Class A constructor

This means that the execution order of non-static initialization blocks precedes the constructor.

classFatherstatictest {Static{System.out.println ("Executes the static code block of the parent class. "); } fatherstatictest () {System.out.println ("Executes a constructor with no arguments to the parent class. "); } fatherstatictest (String str) {System.out.println ("Executes a constructor with parameters for the parent class. "+str); }    {        inti = 1; intj = 1; intsum = (i+j); System.out.println ("Executes the construction code block of the parent class. "+sum); }}classSonstatictestextendsfatherstatictest{Static{System.out.println ("Executes a static block of code for the subclass. "); } sonstatictest () {System.out.println ("Executes a subclass without parameters of the constructor method. "); } sonstatictest (String str) {Super(str); System.out.println ("A constructor that executes a subclass with parameters. "+str); }    {        inti = 2; intj = 2; intsum = (i+j); System.out.println ("Executes the construction code block of the subclass. "+sum); }} Public classSuperclass { Public Static voidMain (string[] args) {NewSonstatictest ("a"); }}

After execution you can see the following order:

executes a static block of code for the parent class. Executes a static block of code for the subclass. Executes the construction code block of the parent class. 2 executes a constructor with parameters for the parent class. A constructs a code block that executes a subclass. 4 The constructor that executes the sub-class with parameters. A

The execution order of static code blocks, non-static code blocks, and construction methods between Java child parent classes

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.