I small white one, look at the initialization of the Java class Dizzy said, I think that although the book is right, but always feel that some information did not say, not clear, read a lot of articles blog, now some sentiment, to lower case summary, but also for the next review of a good fruit.
Pick up the book first:
load : Reads the class file of the classes into memory and creates a Java.lang.class object for it.
Connection : Merges the class's binary data into the JRE, checking that the loaded class has the correct internal structure and is consistent with the other classes. Allocates memory for the static field of the class , sets the default value , and replaces the symbolic reference in the binary data of the class with a direct reference.
initialization : Initializes the static field, initializing it in two ways: the value specified when declaring a static field, and specifying the initial value with a static initialization block. The JVM executes in their order. Initialization consists of the following steps:
If the class is not loaded and connected, the class is loaded and connected first.
If his parent class is not initialized, initialize his parent class first
If there are initialization statements in the class, the system executes sequentially.
After reading it, I had a few questions hovering over my head:
1. What is the relationship and difference between the loading and initialization of the class and the constructor function of the divine horse?
2. Does initialization of a class allocate memory for instance properties?
3. What is the creation process if I want to create an instance object?
OK, if you have the above questions, then let's see what's going on! I'll answer the last question directly, and when you get to the point, the front will be solved.
public class person{
static{staticint = 6; }
{vInt =15; }
static int staticint = 3;
int vInt = 10;
Person () {
staticint = 9;
VInt = 20;
}
}
Person p = new person ();
The JVM will look at:
1) Hey, there's a variable p, and then you assign it a space (the space here refers to the pointer, not the actual object)
2) After the end found that it needs the person of this class to instantiate, and then into the memory to find, see if this class has been loaded into memory, if saved directly to use, if not it will be loaded. We don't have a situation.
3) When loading, read the class file into memory and create a Java.lang.class object for it. It's important to point out that when you create this object, all the information about the class is saved, such as what properties of the class are (static non-static included), what methods ( static and non-static ), and what blocks of code are recorded.
4) Merge the binary data of the class into the JRE, check if the loaded class has the correct internal structure, and coordinate with other classes. The JVM goes to the Java.lang.class object to see what static variables are, allocate memory for them, set default values , and replace the symbolic references in the binary data of the class with a direct reference.
5) The static field is then initialized in two ways: The value specified when the static field is declared, and the initial value is specified with a static initialization block. The JVM executes in their order. this place needs to be noted : As with the code above, the static variable staticint will eventually be assigned a value of 3, because the static code block precedes the declaration. The JVM is to run to the Java.lang.class object to see what static variables, give him a space, and then execute the declaration and static code block statements, so the value after initialization is 3.
Well, 345 are all classes loaded and initialized, let's see what we've done: Build The Java.lang.class object that has all the information for the property method code block in the class. The static property is then allocated memory and a static block of code is executed, as
The order initializes the static property. There is no memory allocated for non-static properties, and no constructors and non-static blocks of code are executed, in a word
The summary is: record all the properties of this class and method code block information, for the static variable memory and assign value .
After initialization, the JVM is now instantiated based on this initialized class information. A static variable that was previously initialized is shared by all instances, and the static code block is no longer executed, which is equivalent to failure. Let's see what the JVM is going to do next.
6) The JVM runs to the Java.lang.class object and takes a look at what instance variables need to be allocated memory, similar to static variables, the JVM first vint the instance variable into memory, then executes the code block and declaration, where it is 10.
7) The last execution of the constructor, after execution,Vint became astaticint, and then changed to 9, then the constructor implicitly returns a person instance object to the variable p.
Okay, we're done, let's see 12 more questions.
1. What is the relationship and difference between the loading and initialization of the class and the constructor function of the divine horse?
Constructors are executed only when the class needs to be instantiated. The loading and initialization of classes is performed in these cases: creating an instance calls a static method to access a static field ( if the variable is final, it is determined at compile time, it is not initialized ) Initializes a class subclass
2. Does initialization of a class allocate memory for instance properties?
Instance properties are not assigned and are only divided into memory when instantiated
Summarize
1) read the class file into memory and create a Java.lang.class object for it.
2) Merge the binary data of the class into the JRE, check that the loaded class has the correct internal structure, coordinate with other classes, and divide memory for static variables
3) assigning values to static variable initialization
The above is the initialization order
4) Divide memory for non-static variables and assign values
5) constructor to return the constructed object
If there is a subclass parent class relationship:
The class files for the parent and child classes are Load into memory, initialize static when parent class, and subclass has static, initialize subclass static,
re-initializes other member variables of the parent class ---Parent class construction Method--subclass other member variable--the constructor method of the child class.
There is no place to look at the master point Ah
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Initialization and constructors for Java classes