A simple example of initialization block, static initialization block, and the execution sequence of the constructor is as follows:
The parent class is as follows:
[Java]
Package test. jamin;
/**
* ClassName: Father
*
* @ Author wangjieming
* @ Version 1.0
* @ Since v1.0
* @ Date 2012-7-2 2:13:57 pm
*/
Public class Father {
Static {
System. out. println ("Father Static Block ");
}
{
System. out. println ("Father Block ");
}
Public Father (){
System. out. println ("Father Constructor ");
}
}
Subclass:
[Java]
Package test. jamin;
/**
* ClassName: Child
*
* @ Author wangjieming
* @ Version 1.0
* @ Since v1.0
* @ Date 2012-7-2 2:16:42 pm
*/
Public class Child extends Father {
Static {
System. out. println ("Child Static Block ");
}
{
System. out. println ("Child Block ");
}
Public Child (){
System. out. println ("Child Constructor ");
}
Public static void main (String [] args ){
Child c = new Child ();
}
}
Analysis:
Subclass inherits the parent class:
First, call the static initialization block of the parent class, and then call the static initialization block of the subclass.
-------------------------------- Split line -----------------------------------
If the child class is instantiated, the parent class initialization block and the parent class constructor are called. Then, the Child class initialization block and the Child class constructor are called.
The running result is as follows:
Author: jamin0107