Java class loading process
I have already written "static code blocks, non-static code blocks, and the loading sequence of constructors". Next I will look at the loading process of java classes.
First look at the Parent class code:
Public class Parent {private int a = 10; public int B; public Parent () {System. out. println ("this is the value of a in the constructor of the parent class:" + a + "B:" + B); B = 188 ;} static int x = print ("this is the parent class when initializing static variable x! "); Public static int print (String str) {System. out. println (str); return 168 ;}}
Child Code:
Public class Child extends Parent {private static int a = print ("this is a subclass of static variable a"); public Child () {System. out. println ("this is the value of constructor a in the subclass:" + a + "B:" + B); B ++ ;} private static int y = print ("this is a subclass in initializing the static variable y! "); Public static void main (String [] args) {System. out. println (" sequence started to test the loading sequence of classes "); new Child ();}}
Execution result:
This is the parent class in initializing static variable x! This is subclass. initialize static variable a. This is subclass. initialize static variable y! Begin starts to test the loading sequence of the class. This is the value of a in the constructor of the parent class. The value of 10 B is: 0. This is the value of constructor a in the subclass: 168 B: 188
Output result analysis: the output statement "begin starts the Loading Order of the test class" is after the initialization of three static variables, it is because when a class is loaded, it recursively loads static variables and static code blocks of the parent class, and then loads static variables of the subclass and static code blocks of the subclass. Static variables and static code blocks are in the same order and initialized in the encoding order. Non-static variables, non-static code blocks, and constructor are loaded only after the instance object is generated. So when we run a program, we first recursively Load Static variables and static code blocks. Of course, this order is recursively loaded. So first output "this is the parent class static initialization static variable x", then output "this is the subclass in Initialization variable a" and "this is the subclass in initializing static variable y !". Then, output "begin to test the loading sequence of the class", and then start to execute new Child () to create an instance. Because the parent class has no non-static code block, only the construction method and non-static variables are available, therefore, Class a is initialized to 10 in the encoding order, Class B is initialized to 0, and the constructor of the parent class is executed. This is also the reason why the output value of a in the constructor of the few parent classes is 10 instead of 168, because the instance is reinitialized during instance creation. After initialization of the parent class constructor and non-static variables, initialize the constructor and non-static variables in the subclass in the same order. Of course, this is also initialized in the encoding order. Some people may wonder why the output result a is 168 and B is 188. This is because the modifier of a is private, so a in the subclass has nothing to do with a in the parent class. They only have the same name. Why is a = 168 here? This is because the subclass calls the print () method of the parent class to assign a value of 168 to a when initializing static variable. As for B, it is because the modifier of B is public and the value can be taken from the parent class to the subclass, So B is 188.
Loading Sequence of summary classes:
1. recursively Load Static variables, static code blocks, and static methods in the class in the encoding order.
2. Perform recursive loading of non-static variables, non-static code blocks, and non-static methods in the class according to the encoding sequence during instantiation.
Reference: http://wenku.baidu.com/link? Url = Response
Statement:
1. original content. For reprinted content, enter the original text link.
2. Don't write a blog. All the content is just a notebook. If you have any mistakes or have better comments, please correct me.
Welcome to xingzhemoluo, share your programming experience, and scan the QR code below;