The initialization order of the parent and subclass classes in Java

Source: Internet
Author: User

Order

1. Static member variables and static code blocks in the parent class

2. Static member variables and static blocks of code in subclasses

3. Common member variables and code blocks in the parent class, constructors for the parent class

4. Generic member variables and code blocks in subclasses, constructors for subclasses

Where the "and" word ends are executed in code order.

Example

Look at the code first:

Father Class

[Java]View PlainCopy
  1. Public class Father {
  2. Public String fStr1 = "Father1";
  3. protected String fStr2 = "Father2";
  4. private String FSTR3 = "Father3";
  5. {
  6. System.out.println ("Father common block be called");
  7. }
  8. Static {
  9. System.out.println ("Father static block be called");
  10. }
  11. Public Father () {
  12. System.out.println ("Father constructor be called");
  13. }
  14. }
The first is the Father class, which has a default constructor, a static block of code, and a common code block for easy viewing of the results.

Son class

[Java]View PlainCopy
  1. Package com.zhenghuiyan.testorder;
  2. Public class Son extends father{
  3. Public String SStr1 = "Son1";
  4. protected String SStr2 = "Son2";
  5. private String SSTR3 = "Son3";
  6. {
  7. System.out.println ("Son common block be called");
  8. }
  9. Static {
  10. System.out.println ("Son static block be called");
  11. }
  12. Public Son () {
  13. System.out.println ("Son constructor be called");
  14. }
  15. public static void Main (string[] args) {
  16. new Son ();
  17. System.out.println ();
  18. new Son ();
  19. }
  20. }

The content of the son class is basically consistent with the Father class, except that son inherits from father. The class has a main function that is intended for testing purposes only and does not affect the result.

Instantiate son in the main function.

The result is:

[Java]View PlainCopy
    1. Father static block be called
    2. Son static block be called
    3. Father Common block be called
    4. Father constructor be called
    5. Son Common block be called
    6. Son constructor be called
    7. Father Common block be called
    8. Father constructor be called
    9. Son Common block be called
    10. Son constructor be called

Summarize:

1, executes the static code block of the parent class when the class loads, and executes only once (because the class is loaded only once);

2, executes the static code block of the subclass and executes only once (because the class is loaded only once);

3, the class member of the parent class is initialized and is executed from top to bottom in order of occurrence (as can be seen at debug).

4, executes the constructor of the parent class;

5, class member initialization is performed for subclasses, and is performed from top to bottom in the order in which they appear.

6, executes the constructor of the subclass.

The initialization order of the parent and subclass classes in Java

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.