Java class loading sequence and java Loading Sequence
Java class loading sequence
I. Loading Sequence
1. initialize static member variables of the parent class
1.1 static code block
1.2. Common Code Block
1.3 No parameter Constructor
2. Static code block of the parent class
3. initialize static member variables of subclass
3.1 static code block
3.2. Common Code Block
3.3 No parameter Constructor
4. Static code blocks of child classes
5. initialize common member variables of the parent class
5.1 Static code block
5.2. Common Code Block
5.3 No parameter Constructor
6. Common Code blocks of the parent class
7. No parameter constructor of the parent class
8. Common member variables of child classes
8.1 static code block
8.2. Common Code Block
8.3 No parameter Constructor
9. Common Code blocks of sub-classes
10. No parameter constructor for subclass
Ii. Sample Code
Super parent class A, parent class B, sub-class C, B inherit A, C inherit B;
AA, AAA, BB, BBB, CC, and CCC all have their own common and static member variables, common, static code blocks, and no-argument constructors.
Super parent class A's common member variable aa (AA class instance), super parent class A's static member variable aaa (AAA class instance );
Normal member variable bb of parent class B (BB class instance), static member variable bbb of parent class B (BBB class instance );
The common member variable cc of subclass C (an instance of the CC class) and the static member variable ccc of subclass C (an instance of the CCC class );
1. Super parent Class
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title
* @ Describe super parent Class
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class {
// Common member variable
Private AA aa = new AA ();
// Static member variable
Private static AAA aaa = new AAA ();
// Static code block
Static {
System. out. println ("I Am A static code block of super parent class ");
}
// Common code block
{
System. out. println ("I am A normal code block of super parent class ");
}
// Construction method without Parameters
Public (){
System. out. println ("I am A super parent class A's no-argument constructor ");
}
}
2. Parent Class B
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title B
* @ Describe parent class
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class B extends {
// Common member variable
Private BB bb = new BB ();
// Static member variable
Private static BBB bbb = new BBB ();
// Static code block
Static {
System. out. println ("I Am a static code block of the parent class B ");
}
// Common code block
{
System. out. println ("I am a normal code block of the parent class B ");
}
// Construction method without Parameters
Public B (){
System. out. println ("I am the construction method without parameters of the parent class B ");
}
}
3. Subclass C
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title C
* @ Describe subclass C
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class C extends B {
// Common member variable
Private CC cc = new CC ();
// Static member variable
Private static CCC ccc = new CCC ();
// Static code block
Static {
System. out. println ("I Am a static code block of subclass C ");
}
// Common code block
{
System. out. println ("I am a normal code block of subclass C ");
}
// Construction method without Parameters
Public C (){
System. out. println ("I Am a sub-class C construction method without Parameters ");
}
}
4. AA class
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title AA
* @ Describe normal member variable AA of super parent Class
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class AA {
// Static code block
Static {
System. out. println ("I Am A static code block of the normal member variable AA of super parent class ");
}
// Common code block
{
System. out. println ("I'm A normal code block of super parent class A's normal member variable AA ");
}
// Construction method without Parameters
Public AA (){
System. out. println ("I am the normal member variable AA of super parent class A without parameters constructor ");
}
}
5. AAA class
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title AAA
* @ Describe static member variable AAA of super parent Class
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class AAA {
// Static code block
Static {
System. out. println ("I am the static code block of AAA, the static member variable of super parent class ");
}
// Common code block
{
System. out. println ("I am A normal code block of the static member variable AAA of super parent class ");
}
// Construction method without Parameters
Public AAA (){
System. out. println ("I am A super parent class A's static member variable AAA's no-argument constructor ");
}
}
6. BB
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title BB
* @ Describe normal member variable BB of parent class B
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class BB {
// Static code block
Static {
System. out. println ("I Am a static code block of the normal member variable BB of the parent class B ");
}
// Common code block
{
System. out. println ("I am a normal code block of the normal member variable BB of the parent class B ");
}
// Construction method without Parameters
Public BB (){
System. out. println ("I am the normal member variable BB of the parent class B without parameters constructor ");
}
}
7. BBB class
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title BBB
* @ Describe static member variable BBB of parent class B
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class BBB {
// Static code block
Static {
System. out. println ("I am the static code block of the BBB member variable of the parent class B ");
}
// Common code block
{
System. out. println ("I am a normal code block of the static member variable BBB of the parent class B ");
}
// Construction method without Parameters
Public BBB (){
System. out. println ("I Am a static member variable of the parent class B, BBB, no parameter constructor ");
}
}
8. CC type
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title CC
* @ Describe: CC, a common member variable of subclass C
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class CC {
// Static code block
Static {
System. out. println ("I Am a static code block of CC, a common member variable of subclass C ");
}
// Common code block
{
System. out. println ("I am a normal code block of CC, a common member variable of subclass C ");
}
// Construction method without Parameters
Public CC (){
System. out. println ("I am the normal member variable of the subclass C. The non-argument constructor of CC ");
}
}
9. CCC class
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title CCC
* @ Describe static member variable CCC of subclass C
* @ Author Zhang fuchang
* @ Date 5:59:17, January 1, April 3, 2017
*/
Public class CCC {
// Static code block
Static {
System. out. println ("I Am a static code block of the static member variable CCC of subclass C ");
}
// Common code block
{
System. out. println ("I am a normal code block of the static member variable CCC of the subclass C ");
}
// Construction method without Parameters
Public CCC (){
System. out. println ("I am the static member variable CCC of the subclass C without parameters constructor ");
}
}
10. Create a test subclass C: TestC. java
Package cn.com. zfc. lesson05.inherit01;
/**
*
* @ Title TestC
* @ Describe: Create a subclass C
* @ Author Zhang fuchang
* @ Date 6:49:50, January 1, April 3, 2017
*/
Public class TestC {
Public static void main (String [] args ){
// Create a C object
C c = new C ();
}
}
Iii. Test Results
I am the static code block of AAA, the static member variable of super parent Class.
I am A normal code block of AAA, A static member variable of super parent Class.
I am A static member variable of super parent class A. The parameter-free constructor of AAA
I am A static code block of super parent Class.
I am the static code block of the static member variable BBB of the parent class B.
I am a normal code block of the static member variable BBB of the parent class B.
I am a static member variable of the parent class B. The method for constructing BBB without Parameters
I am a static code block of the parent class B.
I am the static code block of the static member variable CCC of subclass C.
I am a normal code block of the static member variable CCC of subclass C.
I am a static member variable CCC of subclass C. The construction method without Parameters
I am a static code block of subclass C.
I am A static code block of the normal member variable AA of super parent Class.
I am A normal code block of super parent class A's common member variable AA
I am the normal member variable AA of super parent class A's no-argument constructor.
I am A normal code block of super parent Class.
I am A non-argument constructor of super parent Class.
I am a static code block of the normal member variable BB of the parent class B.
I am a normal code block of the normal member variable BB of the parent class B.
I am the normal member variable BB of the parent class B, and the construction method without Parameters
I am a normal code block of the parent class B.
I am the construction method without parameters of the parent class B.
I am a static code block of CC, a common member variable of subclass C.
I am a normal code block of CC, a common member variable of subclass C.
I am a normal member variable of the subclass C. No parameter constructor of CC.
I am a normal code block of subclass C
I am a non-parametric constructor of subclass C.
Summary:
First, all classes give priority to base classes.
Second, static member initialization takes priority.
Third, the constructor will be executed only after the member is initialized.
Fourth, initialization of static members and execution of static blocks take place during class loading.
Fourth, Class Object creation and access to static blocks will trigger class loading.