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.