黄舟2017-04-18 10:48:41
When running a java program, each class is loaded only once. It is impossible to reload. So there is no recursion as mentioned in the question.
PHP中文网2017-04-18 10:48:41
It is recommended to first understand the memory in the JVM virtual machine
During the instantiation process of AVA objects, the main ones used include the virtual machine stack, JAVA heap and method area.
After the JAVA file is compiled, it will first be added to the JVM method area. A very important part of the JVM method area is the runtime constant pool - used to store the version, fields, methods, interfaces and other description information of the class file class and Constants and static variables during compilation.
The place where JAVA objects are actually instantiated is in the JAVA heap and virtual machine stack, Object A = New Object(); in actual memory, A is actually equivalent to the name we give to the implementation of the Ojbect class. In object-oriented In programming, just like dogs belong to a category of animals, but we will give a name to distinguish the specific dog. Object is used to mark that A belongs to this class, and A refers to a specific implementation of Object, and New Object is equivalent to creating a specific implementation of this class. So we can understand that an object must first be able to specify the class it belongs to, and secondly it must also be able to specify which specific implementation it refers to.
There are two corresponding implementation methods:
1. Handle access object
2. Direct pointer access object
Direct pointer access object
HotSpot adopts the second implementation method.
Class loading includes 3 steps: loading, link, and initialize
Loading
As shown in the figure above, it is not difficult for us to understand that when an object is instantiated, the JVM will The required object type divides the memory area in the JAVA heap and generates a pointer to the method area object data type to identify the object.
Link
The pointer in the local variable table (also called the local variable table) in the virtual machine stack points to the divided memory area in the JAVA heap. The JAVA virtual machine uses dynamic linking. Only the compiled class file does not store the representation of the final method in memory.
Initialization
Initialization is actually a call to the initialization method in the class file, and its core is a POP/PUSH of the stack frame in the virtual machine stack. It is equivalent to performing the same loading process on the objects in the class.
At this point, the complete instantiation process of an object has been introduced.
The internal initialization sequence of the class is static domain->non-static domain->constructor method
Recommended to read:
[JAVA Notes-Tao] Detailed explanation of object life cycle
[JAVA Notes-Tao] Class initialization understanding
怪我咯2017-04-18 10:48:41
Is the poster thinking too profoundly? Instantiation and declaration definition are 2 concepts and stages.
A2 has several static member variables that are instantiated when declared, and the instantiation process of the class is completed through the constructor.
It looks like there is nesting here, but it is not. The compiler can declare instruction jumps when compiling the instantiation statement, and the jump address is determined during secondary compilation.
In this way, during the class declaration phase, when a, b, c, d are instantiated, the compiler will jump to the constructor block of A2(int i).
Jump to the A() constructor during the class instantiation phase.
To put it bluntly, it just calls the corresponding construction methods at different stages. There is no loop nesting as the author thought.
天蓬老师2017-04-18 10:48:41
When loading classes, the loading order for static variables and constructors is different. Constructor methods are also static methods in nature.
I don’t know much about it, so I’ll take a seat first