The order of static members, constructor and subclass in Java class

Source: Internet
Author: User

It mainly tests the Starting sequence of static members, static code blocks, and constructors in the parent class and subclass. Compile a base class: car, and a subclass, audicar. Add some debugging information to the method. The Code is as follows.

1. The car class code is as follows:

Package com. Test. classloader. Order;

/**
* Test base class
*
* @ Author Rey
*
*/
Public class car {
 
Static {
System. Out. println ("[Car]: static method block! ");
}

/**
* Constructor
*/
Public Car (){
Super ();
System. Out. println ("[Car]: constructor! ");
}

Protected string m_scarname = NULL;

/**
* Auto start
*/
Public void start (){
System. Out. println ("[Car]: Start method! ");
}
 
/**
* Stop
*/
Public void stop (){
System. Out. println ("[Car]: Stop method! ");
}
 
Public static void dostart (){
Static Method in system. Out. println ("[Car]: dostart method! ");
}

}

2. the Audi class code is as follows:

/**
*
*/
Package com. Test. classloader. Order;

/**
* @ Author Rey
*
*/
Public class audicar extends car {

Static {
System. Out. println ("[audicar]: static method block! ");
}
 
/**
* Constructor
*/
Public audicar (){
Super ();
System. Out. println ("[audicar]: constructor! ");
}

/**
* Auto start
*/
Public void start (){
System. Out. println ("[audicar]: Start method! ");
}
 
Public static void dostop (){
System. Out. println ("[audicar] Static Method: dostop method! ");
}

}

3. Test the startup sequence of the car and audicar classes. The testcarmain code is as follows:

/**
*
*/
Package com. Test. classloader. Order;

/**
* @ Author Rey
*
*/
Public class testcarmain {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Audicar aaudicar = new audicar ();

Aaudicar. Start ();
}

}

Print result:

In [Car]: static method block!
In [audicar]: static method block!
[Car]: constructor!
[Audicar]: constructor!
In [audicar]: Start method!

 

Conclusion:

1. Execute static members and method blocks in the parent class

2. Execute a large static member and method block in the subclass.

3. Execute the parent class Constructor

4. Execute the subclass Construction Method

5. Execute other members and methods in the parent class

6. Execute other members and methods in the subclass. If the new object is a subclass object, directly execute the subclass method without executing the method with the same name and parameter in the parent class.

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.