The initialization sequence of each member in the java class.

Source: Internet
Author: User

The initialization sequence of each member in the java class.
It is very important to understand the initialization sequence of each class member in java, so that you can have a global understanding of the class. Let's just look at the example below.

Class Father {static {System. out. println ("parent class static code block initialization");} {System. out. println ("parent code block initialization");} private static String s = print (); public static String print () {System. out. println ("parent static method"); return "initialization of the parent static member variable";} public Father () {System. out. println ("parent class without parameter constructor initialization completed"); show ();} public void show () {System. out. println ("parent class show () method");} class Son extends Father {static {System. out. println ("subclass static code block initialization");} {System. out. println ("subclass code block initialization");} private static int I = 1; private String s = "subclass private member variable"; public void show () {System. out. println ("subclass show () method: I =" + I);} public Son () {System. out. println ("subclass constructor initialization completed"); show () ;}} public class TestClassLoadSeq {public static void main (String [] args) {new Son ();}}

  

Output result: the static code block of the parent class initializes the static method of the parent class. The static code block of the subclass initializes the parent class. The class has no parameters. The show () method of the subclass is completed after the constructor is initialized: I = 1 subclass code block initialization subclass constructor initialization completed subclass member variable initialization completed: s = subclass private member variable subclass show () method: I = 1 from the result, we can see that the initialization sequence of each member in the class is: 1. if there is an inheritance relationship in the class (like Son inheriting Father), the base class (Father) of the exported class (Son) will be initialized first, and then the export Class 2. first, the base class initializes static things static blocks> static variables and only initializes them once (because static things are loaded with the class. the static things of the initialization and export class are the same as the static initialization of the base class (same as above) 4. initialize the base class constructor without parameters (initialize the constructor if parameters are called) 5. the initialization and export class has no parameter Constructor (Note: The member variables and code blocks of the export class are both earlier than the initialization of the constructor .. As you can see from the output results) in the above output results, you may see that the show () method is called in the constructor of the base class. This output may not be surprising. I will change it now.
1 class Father 2 {3 private static String s = print (); 4 5 static {6 System. out. println ("parent class static code block initialization"); 7} 8 {9 System. out. println ("parent code block initialization"); 10} 11 public static String print () 12 {13 System. out. println ("parent static method"); 14 return "initialization of the parent static member variable"; 15} 16 public Father () 17 {18 System. out. println ("parent class without parameter constructor initialization completed"); 19 show (); 20} 21 public void show () 22 {23 System. out. println ("parent class show () method"); 24} 25} 26 class Son extends Father27 {28 static {29 System. out. println ("subclass static code block initialization"); 30} 31 {32 System. out. println ("subclass code block initialization"); 33} 34 private int I = 1; 35 private String s = "subclass private member variable"; 36 public void show () 37 {38 System. out. println ("subclass show () method: I =" + I); 39} 40 public Son () 41 {42 System. out. println ("subclass constructor initialization completed"); 43 System. out. println ("subclass member variable initialization completed: s =" + s); 44 show (); 45} 46} 47 public class TestClassLoadSeq {48 public static void main (String [] args) 49 {50 new Son (); 51} 52 53} 54

 

Output result: the subclass static code block initializes the parent class code block. initialization of the parent class without parameters constructor completes the subclass show () method: I = 0 subclass code block initialization subclass constructor initialization completed subclass member variable initialization completed: s = subclass private member variable subclass show () method: I = 1 it can be seen that there is no difference with the above Code. The only difference is that I changed the static member variable I in the export class to the member variable I. It can be seen that the result is completely different, the result is: 0: because the above static member variables are initialized after the class is loaded, the result is 1, and the following is the member variables. Because they are still in the base class constructor, the member variables in the class are exported. the initialization has not been obtained, so it is 0. Therefore, the initialization sequence above is incomplete, now complete: first, when the jvm loads a class, the default initialization for each object is 0, and the default initialization for the reference type through combination is null. Summary:In java, initialize the base class before initializing the export class (How many base classes are initialized) if there are any mistakes, I hope you can help us to learn together (You are also learning ....). Tip: the initialization sequence of each member in java is very important, and a good grasp is very helpful for future study.

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.