Java Object creation Process

Source: Internet
Author: User

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

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.