Java Program Execution Order

Source: Internet
Author: User

Originally I have not figured out the Java program Execution order problem, today, I wrote a test, sure enough with their own consideration of the gap

Test code:

A parent class animal

A sub-class dog

Testing class Test

Operation Result:

So the order of execution is:

Parent class Animal Static code block, subclass dog Static code block, parent class animal non-static code block, parent class animal constructor, subclass dog, non-static code block, subclass, dog constructor

Explore: Why this is the order of execution

Let's start by looking at what happens if you don't use inheritance:

Static code blocks--non-static code blocks--constructors

A static block of code that executes as the class is loaded, executes only once, and takes precedence over the main function execution, which initializes the class.

Non-static block of code : Initializes all objects.
The object runs as soon as it is established, and takes precedence over the constructor execution.

A code block is a unified initialization of all objects ,
The constructor is initialized to the corresponding object .
The initialization of different object commonalities is defined in the code block.

We'll look at the succession,

The new in the main method in 1.Test triggers the JVM to load the class, because the dog inherits the animal, so the animal is loaded first,

Executes all initialization statements or initialization blocks of the static domain (in the order in which they appear), at which point the print statement for the static block in animal executes;

When the parent class's method table is established, the parent class is loaded, and then the subclass dog is loaded, and the procedure is similar to the parent class. Because the subclass does not have a static data field, it executes only the print statements within the static initialization block;

(static initialization is only one time, because it is only executed when the class is loaded and is not related to instantiation )

2. After the required class is loaded, new Dog () starts using the class constructor to create the instance

Executes a non-static block of code for the parent class, initializes it,

Then executes the parent class constructor

Executing subclasses of non-static code blocks

Execute subclass Constructor

So in summary, the following results will appear:

Summary: static code blocks are always executed first.

A non-static block of code, like a non-static method, is related to an object. Only non-static blocks of code are executed before the constructor.

Non-static code blocks and constructors for subclasses begin executing when the parent class is non-static and the constructor finishes executing (equivalent to the initialization of the parent class object)

Java Program Execution Order

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.