Member initialization order for Java classes

Source: Internet
Author: User

To do a simple transcript, it is to revisit the basics.

1. Look at the code first:

  

Package Com.test;public class Test {public    static void Main (string[] args) {        Son s = new Son ()}    } class Par ent{int parent = ten;    {        System.out.println ("initialization block in parent");    }    static{        System.out.println ("Static initialization block in parent");    }         Public parent () {        System.out.println ("Parent constructor Method");}    class Son extends parent{int son = ten;    {        System.out.println ("initialization block in Son");    }         static{            System.out.println ("Static initialization block in Son");            }         Public son () {        System.out.println ("son constructs Method");    }     }

Execution Result:

  

2. Variables and static variables

From the above we can see the static statement block and the execution order of the constructors, then how to instantiate the member variables and static variables?

Visible instance members and instance initialization blocks, executed sequentially in the order in which they appear in the code

3. Summary

Properties, methods, construction methods, and free blocks are members of the class, and the order in which members of the class are executed when the object of the class is created:
1). The parent class static members and static initialization are fast, executed sequentially in the order in which they appear in the code.
2). Subclass static members and static initialization blocks, executed sequentially in the order in which they appear in the code.
3). Instance members and instance initialization blocks of the parent class are executed sequentially in the order in which they appear in the code.
4). Executes the construction method of the parent class.
5). Subclass instance members and instance initialization blocks, executed sequentially in the order in which they appear in the code.
6). The constructor method of the subclass is executed.

Member initialization order for Java classes

Related Article

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.