Java object initialization and java object initialization

Source: Internet
Author: User

Java object initialization and java object initialization

Object A creation process:

   

1. the constructor is actually A static method. Therefore, when the static method/static domain of the object A or Class A is accessed for the first time, the Java interpreter looks for the class path

   

A. class file.

   

2. Load A. class. All actions related to static initialization are executed (). Therefore, static Initialization is only performed once when the Class object is loaded for the first time.

   

3. When creating an object with new A (), allocate enough storage space for object A on the heap.

   

4. the bucket is cleared, so that all basic data of object A is automatically set to the default value, and the reference is set to null.

   

5. Execute all initialization tasks that appear in the field definition.

   

6. Execute the constructor.

 

Attributes, methods, constructor methods, and free blocks are all members of the class. When creating class objects, the execution sequence of the members of the class is as follows:

   

1. The static members and static initialization of the parent class are fast and executed in sequence when the code appears.
2. Subclass static members and static initialization blocks are executed in order of code appearance.
3. The instance members and instance initialization blocks of the parent class are executed in order of code appearance.
4. constructor of the parent class (if it is not explicitly specified to call the constructor of a parent class, the default constructor of the parent class will be called by default. If there is no default constructor, the compiler will

Error)
5. Subclass instance members and instance initialization blocks are executed in order of code appearance.
6. constructor of subclass.

 

 

An effective criterion for writing constructor: (Java programming ideology Fourth Edition Chinese P163)

"Use the simplest possible method to make the object normal. If possible, avoid calling other methods." The only method in the constructor that can be safely called is the final method in the base class.

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.