Constructors are static methods.
1. In Java, when you create an object with new (), the Java Virtual machine first checks to see if the parameters of the new directive are able to navigate to the symbolic reference of the class in the constant pool of the method area, and check that the class that the symbol reference represents is loaded, parsed, and initialized. If not, you must perform the appropriate class loading process.
2. The class loading process is: The Java interpreter looks for the classpath to locate the. class file. The. Class is then loaded and all actions on static initialization are performed. Therefore, static initialization is done only once when the class object is first loaded.
3. After the class load check passes, the virtual machine allocates memory for the new object. The size of the memory required for an object is fully determined after the class is loaded. Allocating memory for a newborn object is the partitioning of a certain size of memory in the Java heap.
4. After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to a value of 0. If you use Tlab (thread local Allocation buffer, which is used to allocate buffering locally), this work process can also be done in advance to Tlab allocation. Initializing to a value of 0 guarantees that an instance field of an object can be used directly without assigning an initial value in Java code, the program accesses the 0 value corresponding to the data type of these fields, the reference is set to NULL, and the base data type is set to 0.
5. Next, the virtual machine makes the necessary settings for the object, such as which class the object is an instance of, how to find the metadata information for the class, the hash code of the object, the age of the object's GC, and so on, which is stored in the object's header.
6. Perform all initialization operations that appear at the field definition.
7. Execute the constructor.
Java Object creation Process