Java Object Initialization Detailed Process
Classification Programming Technology
The process of initializing a class and its objects
I. When to initialize a class
When an object is first created:
Dog dog = new Dog();
When a static method or static field of a class is first accessed:
Dog.staticFields;
The Java interpreter will then look for the class path and locate the compiled Dog.class file.
II. Acquiring Class Resources
Then the JVM will load Dog.class, generate a class object. At this time, if there are static methods or variables, static initialization actions will be executed. Note that static initialization will only run once during the program execution when the Class object is first loaded. These resources will be placed in the JVM's method area.
The method area, also known as the static area, is shared by all threads, just like the heap.
The method area contains elements that are unique throughout the entire program, including all classes and static variables.
III. Initializing the object Dog dog = new Dog()
- The first time a Dog object is created, perform the first two steps above.
- Allocate sufficient storage space for the Dog object on the heap, and all properties and methods are set to default values (numbers to 0, characters to null, booleans to false, and all references are set to null).
- Execute the constructor to check for a parent class; if there is a parent class, the parent class's constructor will be called first. Here, it is assumed that Dog has no parent class, and execute the default value field assignment, i.e., the method's initialization action.
- Execute the constructor.
Initialization in the case of a parent class
Assumption: Dog extends Animal
- Execute the first step, find the Dog.class file, and then during the loading process, it is discovered that it has a base class (through the extends keyword), so first execute the first two steps of the Animal class, load its static variables and methods, and after loading is finished, load the static variables and methods of the subclass Dog.
If the Animal class also has a parent class, it will proceed in this manner, with the final base class being called the root class.
Note: Because the static initialization of the subclass may depend on the static resources of the parent class, the static resources of the parent class should be loaded first.
Next, to create a new Dog object, first allocate storage space for the Dog object -> go to the Dog constructor -> create default properties. Here, the first line in its constructor has an implicit or explicit super(), i.e., the parent class constructor, so at this point, it will jump to the constructor of the parent class Animal.
Dog() { // Create default properties and methods // Call the parent class constructor super() (can be explicitly written) // Assign values and initialize default properties and methods }
Before the parent class Animal executes the constructor, it also allocates storage space -> goes to its constructor -> creates default properties -> finds out that I no longer have a parent class, and at this point, assign default values to its properties and initialize methods.
Then execute the remaining part of the constructor, and after finishing, jump back to the constructor of the subclass Dog.
The subclass Dog assigns values and initializes default properties and methods, and then completes the remaining part of the constructor.
I. Why is it necessary to execute the parent class Animal's constructor before continuing with the property and method assignments of the subclass Dog?
Because the initialization of the non-static variables and methods of the subclass Dog may use the properties or methods of its parent class Animal, so after the subclass constructor defaults the properties and methods, it should not assign values, but should jump to the parent class constructor to complete the construction of the parent class object, and then initialize its own properties and methods.
This is also why the subclass constructor must explicitly call the parent class constructor super() at the first line, as the program needs to jump to the parent class constructor to complete the construction of the parent class object before executing the remaining part of the subclass constructor.
II. Why initialize properties and methods before executing the other parts of the constructor?
Because the explicit part of the constructor may use the object's properties and methods.
Tips: In fact, this initialization process is to ensure that the resources used in the subsequent initialization have been initialized beforehand. It's amazing, worship the fathers of Java.
After so much talk, let's give an example.
Note that the main function is also a static resource, and executing the main function of the Dog class is to call the static resources of Dog.
Example
```
// Parent class Animal
class Animal {
/