Process at initialization

Source: Internet
Author: User

The working steps of the JVM when you new an object:

1: Define the method when the stack memory defines the variable at this time as the initial value. Basic data type int 0. The reference data type is null;

2: Call the Parent class construction method, define the parent class's properties and methods (such as the fruit class has overridden the parent class method at this time will not be overwritten, the entire process will not occur any overwrite).

The private method of the parent class cannot be overridden, and you change the parent class's getnum to protected and private results are not the same!!

3: Assigns a value to the variable of the parent class.

4: Executes other statements in the parent constructor method (at this point its own variable has been initialized and the assignment is complete, seemingly reasonable).

5: Assign yourself a variable (create an object or constant in heap memory).

6: Execute other statements in the constructor method.

The actual process of initialization is:
1. Initialize the storage space allocated to the object to binary zero before anything else occurs.
2. Call the base class constructor. This step is repeated recursively, first of all to construct the root of this hierarchy, then the next layer of the export class, and so on. The export class until the lowest layer.
3. Invoke the initialization code of the member in the order in which it is declared. Within the class, the order of initialization is first "static", (if they have not been initialized because of the previous object creation process), then "non-static". The order in which non-static variables are defined determines the order in which they are initialized. Even if the variable definitions are scattered between the method definitions, they will still be initialized before any methods (including constructors) are called.
4. Call the constructor body of the exported class.

If you want to invoke a method in the constructor, declare the method as private.
For this rule it is necessary to note that if you are calling a non-static method in your parent class constructor, this method is not overloaded with private and quilt classes, so when the constructor of the parent class is called recursively during the actual creation of the subclass, The parent constructor's call to this method will actually call the subclass's method due to polymorphism, and when the subclass method needs to use the instance variable in the subclass, the exception will occur because the variable is not initialized (as to why the instance variable in the subclass is not initialized to refer to the instance initialization process above). This is what Java doesn't want to see. When a method called in the parent class constructor is a private method, polymorphism does not occur, and the parent class constructor calls the subclass method, which guarantees that the parent class always calls its own method. Even if an instance variable in the parent class is called in this method, there is no case where the variable is uninitialized (the initialization of the variable is always performed before the current class constructor body executes).

Process at initialization

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.