Java neutron classes inherit the parent class, and the program runs in an in-depth analysis of the sequence _java

Source: Internet
Author: User
Tags inheritance
We often use inheritance in our projects, but we often don't quite understand the sequence and principle of the program running, especially when using the transition object, and when there are static variables and methods in the parent subclass, you don't know who to run first. I have also written an example. summed up a bit.
Copy Code code as follows:

Parent class:
public class Teststatic {

public static String Name= "the";

{
System.out.println ("======== method body ========");
}

static{
Name= "England";
System.out.println ("======== static program block = =");
}

Teststatic () {
SYSTEM.OUT.PRINTLN ("========= construction Method ========");
}

public static void Main (string[] args) {
SYSTEM.OUT.PRINTLN ("======== Main method ========" +name);
}

public void Test () {
System.out.println ("======== test Method =========");
}
}

Sub Class:
public class Testextendstatic extends teststatic{

public static String name= "Hubei";

{
System.out.println ("======== No Name Method body ========");
}

static{
Name= "Suizhou";
System.out.println ("======== static program block = =");
}

Testextendstatic () {
SYSTEM.OUT.PRINTLN ("========= construction Method ========");
}

public void Test () {
System.out.println ("======== test Method =========");
}

public static void Main (string[] args) {
System.out.println ("======== sub-class Main method ========" +name);
Teststatic ts = new testextendstatic ()//Transition Object
Ts.test ();
}
}
The output is as follows:
======== static program block = = = Parent class Static program block
======== Subclass Static Program block = = = = Sub Class static program block "not static method"
======== Subclass Main Method ========england: Subclass Main Method
======== method Body ========: Non-static code block of parent class
========= Construction Method ========: Parent class Construction method
======== non-name method body ========: Non-static code block for subclasses
========= Subclass Construction Method ========: Subclass Construction Method
======== sub-class test method =========: Subclass test method

Order of execution:Static variables of parent class and static program block---Subclass and static program block---Parent class non-static code block---The constructor method in the parent class---subclass static code block---Subclass constructor---Next is the side of the object invocation
Method.
As long as the object is created with new, the memory space is allocated, whether the reference is assigned to a transition object or to a subclass object, the above method must be executed.
namely: teststatic ts = new testextendstatic ()//Transition Object
Testextendstatic ts = new testextendstatic ()//Child class object
The above bold program will execute.

Ts.test () in the above program; TS as a Transition object invokes a method in the parent class that inherits from the subclass, because test () is overridden in a subclass, so the output is the statement in the subclass.

If you include the main method in the subclass as follows:
Copy Code code as follows:

public static void Main (string[] args) {
System.out.println ("======== sub-class Main method ========" +name);
Teststatic ts = new testextendstatic ();
Ts.test ();
System.out.println ("-------------------------");
ts = new testextendstatic ();
Ts.test ();
}
output:
======== static program block = = Static program block in parent class
======== Subclass Static Program block = = = Static program block in subclass
Main method of ======== subclass Main method ========england Subclass
======== method body ======== Non-static code block of parent class
========= Construction Method ======== The construction method in the parent class
======== non-static program block for ======== subclass
Constructing method of ========= subclass in ======== subclass
======== Subclass test Method ========= Object-specific invocation method
-------------------------static variables and the program block executes only once
======== method body ======== Non-static code block of parent class
========= Construction Method ======== The construction method in the parent class
======== non-static code block for ======== subclass
Constructing method of ========= subclass in ======== subclass
======== test method for sub-class =========

If you change the subclass Main method to:
Copy Code code as follows:

Teststatic ts = new teststatic ()//Using parent class construct method to create
Ts.test ();
The output is:
======== static program block = = Parent class Static program block
======== Subclass Static Program block = = Class Static program block "because the program is running in subclasses, the static program block of subclasses must run"
======== method Body ======== A non-static program block of a parent class
========= Construction Method ======== Parent class construction method
======== test method ========= Parent class specific method Test ()
If you put the above code in the parent class, the subclass static program block is not loaded.

Through the above we can also find that the static program block runs before the main method, the Non-static block is run after the main method.
I create an object call to test () in the Main method in the parent class, and run the result:
======== static program block = = = Static code block
===main==
======== method body ======== Non-static code block
========= Construction Method ======== Construction method
======== test Method =========

Summary:
When the program runs (in a class), it loads the static code block at the first time, and once the object is created, the Non-static code block and the parameterless construction method are executed. And in the inheritance, when the program runs, it loads the static code block in the parent class and then loads its own static code block, and once the object is created (created using the subclass constructor), the parent class is called a non-static code block, the parent class constructs the method, and then the Non-static code block itself constructs the method.
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.