JAVA initialization sequence, java Initialization

Source: Internet
Author: User

JAVA initialization sequence, java Initialization
Main content:

1. JAVA initialization sequence without inheritance

2. initialization sequence in case of JAVA inheritance

JAVA initialization sequence without inheritance
1 class Sample 2 {3 Sample (String s) 4 {5 System. out. println (s); 6} 7 Sample () 8 {9 System. out. println ("Default Sample constructor called"); 10} 11} 12 class Test {13 static Sample sam = new Sample ("static member sam initialization "); 14 Sample sam1 = new Sample ("sam1 member initialization"); 15 static {16 System. out. println ("static block execution"); 17 if (sam = null) System. out. println ("sam is null"); 18 sam = new Sample ("initialize sam member variable in static block"); 19} 20 Test () 21 {22 System. out. println ("Test default constructor called"); 23} 24} 25 // main function 26 public static void main (String str []) 27 {28 Test a = new Test (); 29}
View Code

Output result:

Static member sam initialization-static member Initialization

Static block execution-static block execution

Initialize the sam member variable in the static block ---- execute the static Block

Sam1 member initialization ----- normal member Initialization

Test default constructor called ----- constructor execution

Therefore, we can draw a conclusion:

A Static member variable is initialized first (note that Static can be considered as a Static member, and its execution sequence is related to the sequence stated in the class)

Class B common member Initialization

C. Execute the constructor.

 

For static members (static blocks can be regarded as normal static members, which are not necessarily executed first during class initialization) and normal members, the initialization sequence is only related to the sequence in class definition, it has nothing to do with other factors.

For example:

1 class Test {2 static {3 System. out. println ("static Block 1 execution"); 4} 5 static Sample staticSam1 = new Sample ("static member staticSam1 initialization "); 6 Sample sam1 = new Sample ("sam1 member initialization"); 7 static Sample staticSam2 = new Sample ("static member staticSam2 initialization"); 8 static {9 System. out. println ("static Block 2 execution"); 10} 11 Test () 12 {13 System. out. println ("Test default constructor called"); 14} 15 Sample sam2 = new Sample ("sam2 member initialization"); 16}
View Code

The result is:

Static Block 1 execution

Static member staticSam1 Initialization

Static member staticSam2 Initialization

Static Block 2 execution

-------- Static member

Sam1 member Initialization

Sam2 member Initialization

-------- Common member

The default Test constructor is called.

-------- Constructor

Initialization sequence in the case of JAVA inheritance
1 class Sample {2 Sample (String s) {3 System. out. println (s); 4} 5 6 Sample () {7 System. out. println ("Default Sample constructor called"); 8} 9} 10 11 class Test {12 static {13 System. out. println ("parent class static Block 1 execution"); 14} 15 16 static Sample staticSam1 = new Sample ("parent class static member staticSam1 initialization "); 17 Sample sam1 = new Sample ("parent class sam1 member initialization"); 18 static Sample staticSam2 = new Sample ("parent class static member staticSam2 initialization "); 19 20 static {21 System. out. println ("parent class static Block 2 execution"); 22} 23 24 Test () {25 System. out. println ("the default constructor of the parent class Test is called"); 26} 27 28 Sample sam2 = new Sample ("parent class sam2 member initialization "); 29} 30 31 class TestSub extends Test {32 static Sample staticSamSub = new Sample ("class static member staticSamSub initialization"); 33 34 TestSub () {35 System. out. println ("TestSub default constructor called"); 36} 37 38 Sample sam1 = new Sample ("subclass sam1 member initialization "); 39 static Sample staticSamSub1 = new Sample ("static member staticSamSub1 initialization of subclass"); 40 41 static {42 System. out. println ("subclass static block execution"); 43} 44 45 Sample sam2 = new Sample ("subclass sam2 member initialization"); 46}
View Code

Execution result:

Execute the parent class static Block 1

Initialize staticSam1, a static member of the parent class.

Initialize staticSam2, a static member of the parent class.

Parent class static Block 2 execution

-------- Initialize static members of the parent class

Initialize staticSamSub, a static member of the subclass.

Initialize static sub-class member staticSamSub1

Subclass static block execution

------- Static member initialization of subclass

Parent class sam1 member Initialization

Parent class sam2 member Initialization

The default constructor of the parent class Test is called.

------- Initialization and constructor execution of common members of the parent class

Subclass sam1 member Initialization

Subclass sam2 member Initialization

The default constructor of TestSub is called.

------- Initialization and constructor execution of common members of the parent class

The following figure shows the JAVA initialization sequence:

1. Initialize all static members of the inheritance system (parent class and Child class)

2. parent class initialization is complete (initialization of common members --> calling of constructor)

3 subclass initialization (common member --> constructor)

JAVA initialization sequence

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.