Execution sequencing of inheritance and static statics in Java

Source: Internet
Author: User

Package extend;

public class X {
Y y=new y ();
static{
System.out.println ("tttt");
}
X () {
System.out.println ("X");
}

public static void Main (string[] args) {
New Z ();

}
}

Class y{
Y () {
System.out.println ("Y");
}
}

Class Z extends x{
Y y=new y ();
static{
SYSTEM.OUT.PRINTLN ("tt");
}
Z () {
System.out.println ("Z");
}
}

Let's not tell the final result, we'll analyze it first. A step-by-step rollout of results.

1. First, after analyzing the results of a program, we have to find the entrance to the program before we can proceed with the analysis.

This is the main () method.

2. We found that the main () method in the X class, to execute the main () method, you have to first load the class X into memory.

What does the 3.X class do when it is finished loading? Don't worry, first look at the role of static, do not know it. Tell you: Static is executed when the class is loaded for the first time, and then no longer executes.

4. Knowing the function of static, then the X class is loaded, then the static and static statement blocks (static) of the class X are executed, and the order is executed to see who executes the first. Only at this time, not later.

5. So one output is tttt, no problem.

The static statement block for the 6.X class executes, so it's time to execute the main () method.

7.new Z (); This method is executed.

8. Since new Z (), then the Z class will be loaded. Because the Z class inherits the X class. So you have to load the X class before you can. Because the X class has already been loaded. So there is no need to load the X class at this time. Z class loads the static statement block to execute the Z class

9. Then the TT will be printed out.

10. After the addition of all the objects will be instantiated.

11. Before instantiating Z, you have to instantiate X right. The constructor method of the parent class is called because the child class is constructed.

12. Instantiate the class X first.

13. The X method must be initialized before it is executed. That is, get all the properties. Then the attribute y of the X class is acquired.

14. That is, the X class y y=new y (); to be executed. That is, y is printed.

15. Then execute the System.out.println ("X");
16. Then the construction method of Z is executed.

17. Also get the properties of Z first y y=new y (); Print Y.

18. Re-execution of System.out.println ("Z");


This is the whole process. Now you know the result:

Tttt
Tt
Y
X
Y
Z

Execution sequencing of inheritance and static statics 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.