The execution order of Java classes and inheritance

Source: Internet
Author: User

1. Sub-class

 Public class extends Testclasslifesuper {        public  Testclasslife () {        System.out.println ("3. Number of construction rows accessed" );    }    {        System.out.println ("2. Initialization block is accessed");    }     Static {        System.out.println ("1. Static initialization block is accessed"     );  Public void Go () {             System.out.println ("4. Method Body");}    }

2. Parent class

 Public classTestclasslifesuper { PublicTestclasslifesuper () {System.out.println ("3. The number of parent constructed rows is accessed"); } {System.out.println ("2. Parent initialization block is accessed"); }    Static{System.out.println ("1. The parent static initialization block is accessed"); }    Private StaticString a = "1";  Public voidGo () {String B=A; System.out.println ("4. Parent Method Body"); }}

Final execution Results

1. Parent static initialization block is accessed
1. Static initialization block is accessed
2. Parent initialization block is accessed
3. The number of parent constructed rows is accessed
2. Initialization block is accessed
3. The number of construction rows is accessed
4. Method body

The President logic, first static initialization, then the normal initialization, and then the constructor, the later son of the father. In special cases, the static initialization of a subclass is immediately followed by the static initialization of the parent class.

The execution order of Java classes and inheritance

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.