Java-static code block, constructor code block, constructor, and Java class initialization sequence. constructor java
Static code block: it is declared with staitc. It is executed only once when jvm loads the class.
Construct a code block: the class is defined directly with {} and executed every time an object is created.
Execution sequence priority: static block, main (), construction block, and construction method.
Constructor
Public HelloA () {// constructor}
Note the following points about constructors:
1.Once an object is created, the corresponding constructor will be called.That is to say, if no object is created, the constructor will not run.
2. The role of the constructor is to initialize the object.
3. An object is created and the constructor runs only once. A general method can be called multiple times by this object.
Construct code blocks
{// Construct a code block}
Note the following when constructing code blocks:
Static code block
Static {// static code block}
For static code blocks, note the following:
Public class Test {staitc int cnt = 6; static {cnt + = 9;} public static void main (String [] args) {System. out. println (cnt) ;}static {cnt/= 3 ;}} running result: 5
Java class initialization sequence
# For a class
Example 1:
Public class HelloA {public HelloA () {// constructor System. out. println ("constructor of A") ;}{// construct the code block System. out. println ("construct code block of A");} static {// static code block System. out. println ("static code block of A");} public static void main (String [] args) {}} running result: static code block of
Example 2:
Public class HelloA {public HelloA () {// constructor System. out. println ("constructor of A") ;}{// construct the code block System. out. println ("construct code block of A");} static {// static code block System. out. println ("A static code block");} public static void main (String [] args) {HelloA a = new HelloA () ;}} running result: a's static code block A's construction code block A's constructor
Example 3:
Public class HelloA {public HelloA () {// constructor System. out. println ("constructor of A") ;}{// construct the code block System. out. println ("construct code block of A");} static {// static code block System. out. println ("static code block of A");} public static void main (String [] args) {HelloA a = new HelloA (); HelloA B = new HelloA ();}} running result: constructor A of the static code block
For a class, the execution is performed in the following order:
For static variables, static initialization blocks, variables, initialization blocks, constructors, their initialization sequence is (static variables, static initialization blocks)> (variables, initialization blocks)> constructor.
Example 4:
1 public class InitialOrderTest {2/* static variable */3 public static String staticField = "static variable"; 4/* variable */5 public String field = "variable "; 6/* static initialization block */7 static {8 System. out. println (staticField); 9 System. out. println ("static initialization block"); 10} 11/* initialization block */12 {13 System. out. println (field); 14 System. out. println ("initialization block"); 15} 16/* constructor */17 public InitialOrderTest () 18 {19 System. out. println ("constructor"); 20} 21 22 23 public static void main (String [] args) 24 {25 new InitialOrderTest (); 26} 27}
Run the above code and we will get the following output:
# Inheritance
Example 5:
Public class HelloA {public HelloA () {// constructor System. out. println ("constructor of A") ;}{// construct the code block System. out. println ("construct code block of A");} static {// static code block System. out. println ("A static code block") ;}} public class HelloB extends HelloA {public HelloB () {// constructor System. out. println ("B constructor") ;}{// construct the code block System. out. println ("B's construction code block");} static {// static code block System. out. println ("B's static code block");} public static void main (String [] args) {HelloB B = new HelloB () ;}} running result: A static code block B static code block A construction code block A constructor B construction code block B Constructor
When inheritance is involved, the execution is performed in the following order:
Example 6:
1 class Parent {2/* static variable */3 public static String p_StaticField = "Parent class -- static variable "; 4/* variable */5 public String p_Field = "parent class -- variable"; 6 protected int I = 9; 7 protected int j = 0; 8/* static initialization block */9 static {10 System. out. println (p_StaticField); 11 System. out. println ("parent class -- Static initialization block"); 12} 13/* initialization block */14 {15 System. out. println (p_Field); 16 System. out. println ("Parent class -- initialization block"); 17} 18/* constructor */19 public Parent () 20 {21 System. out. println ("parent class -- constructor"); 22 System. out. println ("I =" + I + ", j =" + j); 23 j = 20; 24} 25} 26 27 public class SubClass extends Parent {28/* static variable */29 public static String s_StaticField = "SubClass-static variable "; 30/* variable */31 public String s_Field = "subclass -- variable"; 32/* static initialization block */33 static {34 System. out. println (s_StaticField); 35 System. out. println ("subclass -- Static initialization block"); 36} 37/* initialization block */38 {39 System. out. println (s_Field); 40 System. out. println ("SubClass -- initialization block"); 41} 42/* constructor */43 public SubClass () 44 {45 System. out. println ("subclass -- constructor"); 46 System. out. println ("I =" + I + ", j =" + j); 47} 48 49 50/* program entry */51 public static void main (String [] args) 52 {53 System. out. println ("SubClass main method"); 54 new SubClass (); 55} 56}
Result:
Parent class-static variables
Parent class-static initialization Block
Subclass-static variable
Subclass-static initialization Block
Subclass main method
Parent class -- Variable
Parent class -- initialize the block
Parent class -- Constructor
I = 9, j = 0
Subclass -- Variable
Subclass -- initialize the block
Subclass -- Constructor
I = 9, j = 20
The initialization of static variables and static initialization blocks of subclass is completed before the initialization of the parent class variables, initialization blocks, and constructors. Static variables and static initialization blocks. The initialization sequence of variables and initialization blocks depends on the sequence in which they appear in the class.
### Analysis
(1) Access SubClass. main (), (this is a static method), so the loader will find the compiled SubClass class code (that is, the SubClass. class file) for you ). During the loading process, the loader notices that it has a base class (that is, the meaning of extends), so it loads the base class. Whether you create a base class object or not, this process will always happen. If the base class has a base class, the second base class will also be loaded, and so on.
(2) execute static initialization of the base class, and then initialize the static initialization of the next derived class, and so on. This order is very important because the "static initialization" of the derived class may depend on the correct initialization of the base class members.
(3) When all necessary classes have been loaded, execute the main () method body and create an object with new SubClass.
(4) If the class SubClass has a parent class, the constructor of the parent class is called. You can use super to specify which constructor to call. The construction process and sequence of the base class are the same as those of the derived class. Initialize each variable in the base class in the literal order, and then execute the rest of the base class constructor.
(5) initialize subclass member data in the order they are declared and execute the rest of the subclass constructor.
Reference Links:
Java-static code block, construction code block, constructor
Java class initialization sequence