Sequence of constructors and free Blocks

Source: Internet
Author: User

Package com. spoto. test;
 

Public class test1 {
 

Private Static string S1 = "parent static variable ";
 

Private string S2 = "parent variable ";
 

Public test1 (){
 
System. Out. println ("parent class constructor ");
 
}
 
/**
 
* Static code block
 
*/
 

Static {
 
System. Out. println ("parent static block ");
 

}
 
/**
 
* Free block
 
*/
 
{
 
System. Out. println (S2 );
 
System. Out. println (S1 );
 
System. Out. println ("parent class free block ");
 

}
 

Public static void main (string [] ARGs ){
 
New testchild ();
 
}
 

}
 

Class testchild extends test1 {
 

Private Static string S1 = "subclass static variables ";
 

Private string S2 = "subclass variable ";
 
/**
 
* Static code block
 
*/
 

Static {
 
System. Out. println ("subclass static block ");
 

}
 
/**
 
* Free block
 
*/
 
{
 
System. Out. println (S2 );
 
System. Out. println (S1 );
 
System. Out. println ("subclass free block ");
 

}
 

Public testchild (){
 
System. Out. println ("sub-class constructor ");
 
}
 

}

Running result:

Parent static Block
Subclass static Block
Parent variable
Parent static variable
Parent free block
Parent class Constructor
Subclass variable
Static subclass Variables
Subclass free block
Constructor of subclass

Reference: http://java.bootcamp.cn/forum.php? MoD = viewthread & tid = 1976.

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.