Execution sequence of Java static variables and constructors

Source: Internet
Author: User

Case:

 Package Com. Devin;

Public Class Mymain {
Public Static Void Main (string ARGs []) {
Father F = New Son ();
}
}

Class Father {
Public Father (){
System. Out. println ("Father construct ");
}

Static Int A = printfahter ();

Static Int Printfahter (){
System. Out. println ("print in Father ");
Return 1;
}
}

Class Son Extends Father {
Public Son (){
System. Out. println ("Son construct ");
}

Static Int B = printson ();

Static Int Printson (){
System. Out. println ("print in Son ");
Return 2;
}
}

Output result:

 
Print in Father
Print in son
Father construct
Son construct

Conclusion: The execution sequence of static variables and constructors is as follows:

1. Static variables of the parent class;

2. Static variables of child classes;

3. constructor of the parent class;

4. constructor of the subclass;

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.