Java object creation process (simple and straightforward)

Source: Internet
Author: User

Suppose you have a cat class named:

1, even if the static keyword is not displayed, the constructor is actually a static method. So when you create an object of type cat, or the static method/static domain of the Cat class is first accessed (referenced), the Java interpreter must look for the classpath (find on Classpath) to locate the Cat.class file.

2, and then the class loader loads the Cat.class, and all actions related to static initialization are performed. Also, static initialization occurs only once when the class object is first loaded (the keyword is static, is bound to the class, and is accessible through the class, and the object of that class shares the resource.) )

3. When you create an object with new Cat (), you first allocate enough storage space on the heap for the Cat object.

4. This storage space is zeroed, which automatically sets all the basic type data in the Cat object to the default value (the number is 0, the Boolean and the character type are the same), and the reference is set to NULL.

5. Perform all initialization actions that appear in the field definition. ( initialized in the order of the statement positions )

6, the execution of the constructor.

Public static void Main (String args[])

This is the entry for the Java program execution and is a static method. When the sentence is executed, the interpreter will look for the modified. class file and load it in (load the parent class, reload the interface, etc.), initialize the static variables of the class first (in the Order of the Code), then initialize the non-static reference object, Finally executes the constructor.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java object creation process (simple and straightforward)

Related Article

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.