The order in which the Java class is initialized:
This concludes the Java generic class initialization sequence:
- static variables
- Static initialization blocks
- Variable
- Initialize block
- Constructors
This concludes the initialization sequence of the Java inheritance classes:1. Initialization of all static members of the inheritance system (late Father class, later subclass)2. Initialization of the parent class (initialization of the normal member---called by the constructor)3. Subclass initialization (normal member---constructors)
Java Initialization OrderCode Demo:
class Sample {Sample (String s) {System.
out. println (s); }Sample () {System.
out. println ("The sample default constructor is called"); }}
class test{ Static
sample
sam=
new sample ("Static member Sam initialization"); Sample sam1=
new sample ("SAM1 member initialization");
static{ System.
out. println ("Static block Execution");
if(
sam=
null) System.
out . println ("Sam is null");
sam=
new Sample ("Initialize SAM member variable within static block"); }Test () {System.
out. println ("test default constructor is called"); } }//Main functionPublic
static
void main (String str[]) /c16>{ Test a=
new test (); }the output is:
static member of SAM initialization -----static member initializationstatic block
execution -----block execution
static block initialization of SAM member variables ----static block execution
SAM1 member Initialization----- normal member initializationThe
test default constructor is called-----constructor executes The conclusion can be drawn from this:a static member variable is initialized first (note that static can be seen as a static member in the order in which it is executed and in the order in which it is declared in the class) b Normal Member initializationC executes the constructor. for static members (a static block can be seen as a normal, statically member, it is not necessarily executed first at class initialization ) and ordinary members, whose initialization order is only related to the order in the class definition, regardless of other factors. For example, the following example:
class test{
static{ System. Out
. println ("Static block 1 Execution"); }
static sample
staticSam1=
new sample (" Static member StaticSam1 initialization "); Sample sam1=
new sample ("SAM1 member initialization");
static sample
staticSam2=
new sample (" Static member StaticSam2 initialization ");
static{ System. Out
. println ("Static block 2 Execution"); }Test () {System.
out. println ("test default constructor is called"); }Sample Sam2=
new sample ("SAM2 member initialization"); } The result is:static block 1 executionstatic member StaticSam1 initializationstatic member StaticSam2 initializationstatic Block 2 execution--------Static MembersSAM1 member InitializationSAM2 member Initialization--------Ordinary MembersThe test default constructor is called--------Constructors 2 initialization sequence in case of Java inheritance:
class test{
static{ System. Out
. println ("Parent class static block 1 Execution"); }
static sample
staticSam1=
new sample ("Parent class Static member StaticSam1 initialization "); Sample sam1=
new sample ("Parent class SAM1 member initialization");
static sample
staticSam2=
new sample ("Parent class Static member StaticSam2 initialization ");
static{ System. Out
. println ("Parent class static block 2 Execution"); }Test () {System.
out. println ("parent class test default constructor is called"); }Sample Sam2=
new sample ("Parent class SAM2 member initialization"); }
class testsub
extends Test {
static sample
staticsamsub=
new sample ("Subclass Static member Staticsamsub initialization "); testsub () {System.
out. println ("Subclass TestSub default constructor is called"); }Sample sam1=
new sample ("Subclass SAM1 member initialization");
static sample
staticSamSub1=
new sample ("Subclass Static member StaticSamSub1 initialization ");
static{System.
out . println ("sub-class static block execution");} Sample Sam2=
new sample ("Subclass SAM2 member initialization"); } Execution Result:Parent class static block 1 executionSTATICSAM1 Initialization of static members of a parent classSTATICSAM2 Initialization of static members of a parent classParent class static block 2 execution --------Static member initialization of a parent class Subclass Static Member Staticsamsub initializationSubclass Static Member StaticSamSub1 initializationsub-class static block execution -------Subclass static member initialization SAM1 member initialization of parent classSAM2 member initialization of parent classThe parent class test default constructor is called -------The normal member initialization and constructor execution of the parent class Subclass SAM1 Member InitializationSubclass SAM2 Member InitializationSubclass TestSub Default constructor is called -------The normal member initialization and constructor execution of the parent class
Android (Java) Learning Note 136:java class initialization order