Java Program Execution Order

Source: Internet
Author: User

Mastiff class

/*** Sub-class Tibetan mastiff*/ Public classMastiffextendsDog { PublicMastiff () {System.out.println ("Mastiff"); } {System.out.println ("Block"); }    Static{System.out.println ("Static Block"); }         Public Static voidMain (string[] args) {Mastiff Mastiff=NewMastiff (); }}

Dog class

/*** *publicclass  dog    {public  Dog () {        System.out.println ("Dog");}    } </span>

The result of the operation is:

Static block
Dog
Block
Mastiff

In other words, in our program, when instantiating a class object, the order of operation is:

    1. Static block
    2. Parent class constructor
    3. Blocks in this class
    4. Constructors for this class

Can we go further, if there are blocks and static blocks in the parent class?

Dog class improved after source code

/*** *publicclass  dog    {public  Dog () {        System.out.println ("Dog");    }     Static {        System.out.println ("Super static Block");    }        {        System.out.println ("Super Block");}    }

Mastiff improved source code

/*** Sub-class Tibetan mastiff*/ Public classMastiffextendsDog { PublicMastiff () {System.out.println ("Mastiff"); } {System.out.println ("Block"); }    Static{System.out.println ("Static Block"); }         Public Static voidMain (string[] args) {Mastiff Mastiff=NewMastiff (); }}

The result of the operation is:

Super static block
Static block
Super Block
Dog
Block
Mastiff

In other words, the order of operation at this time is:

    1. Parent class static Block
    2. Self-static block
    3. Parent Class Block
    4. Parent class constructor
    5. Self block
    6. Self-constructor

Well, knowing the order of the run, then what is this for?

This is going to start with the loading mechanism and instantiation mechanism of the class in the JVM, here because of the subject matter, not discussed first, interested students can check their own information.

Let's discuss the second question, the value of a variable, where it might be determined??

    1. Inherit the value from the parent class (including: 1. A member variable that is a parent class has been assigned a value of 2. Assign a value in the parent class's block 3. Assigning values in the constructor of the parent class)
    2. Assigning a value to a constructor
    3. Assigning values in a block
    4. assigning values in a method call

Now suppose in our just example that there is a variable type that represents the breed of dog

/*** *publicclass  Dog    {public String type= " The value assigned to the parent class member variable ";      Public Dog () {        System.out.println ("parent class constructor--type-->" +type);        Type= "The value assigned by the parent class constructor";                   System.out.println ("parent class constructor----Type--->" +type);    }        {        System.out.println ("Block---type--->" +type);        Type= "The value assigned by the parent class block";}    }
/*** Sub-class Tibetan mastiff*/ Public classMastiffextendsDog { PublicString type= "value assigned to member variable";  PublicMastiff () {System.out.println ("Constructor---Type--->" +type); Type= "constructor-assigned value"; }         Public voidsay () {System.out.println ("Say---type---->" +type); } {System.out.println ("Block---type--->" +type); Type= "Value of block assignment"; }         Public Static voidMain (string[] args) {Mastiff Mastiff=NewMastiff (); Mastiff.say ()</span><span style= "Font-size:medium;" >;</span><span style= "Font-size:medium;" >            }}

The results of the implementation are as follows:

Block---The value assigned to type---> Parent class member variable
Parent class constructor--type--> the value assigned to the parent class block
Parent class constructor----value assigned to type---> Parent class constructor
Block---value assigned to type---> member variable
Constructor---The value assigned to type---> block
Say---type----> constructor-assigned value

The answer is clearly that the order of assignment is:

    1. Parent class Member variable assignment
    2. Parent Class block Assignment
    3. Parent class constructor Assignment value
    4. Assigning values to self-member variables
    5. Self-block assignment
    6. Self constructor Assignment

In combination with the order of execution in the program we said earlier, this is clearly well understood:

1. Member variable assignment >>> block Assignment >>> constructor assignment

2. Parent class block >> parent class constructor >> self block >> self constructor

Also because a member variable is not possible to assign a value in a static variable, and the previous program execution sequence is known

Static blocks >> Blocks

Therefore, the program's assignment step is

      1. Static variable assignment for parent class
      2. Static variable assignment of its own value
      3. Parent class Member variable assignment
      4. Parent Class block Assignment
      5. Parent class constructor Assignment value
      6. Assigning values to self-member variables
      7. Self-block assignment
      8. Self constructor Assignment

Java Program Execution Order

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.