The process of initializing a class and its objects
1. When do you need to initialize a class
When first creating an object:
Dog dog = new Dog();
When first accessing a static method or static field of a class:
Dog.staticFields;
The Java interpreter will look for the class path and locate the compiled Dog.class file.
2. Obtaining class resources
Then the JVM will load Dog.class and generate a class object. At this time, if there are static methods or variables, static initialization actions will all be executed. Pay attention here: static initialization will only run once when the Class object is first loaded during program execution. These resources will all be placed in the JVM's method area.
The method area, also called the static area, is like the heap and is shared by all threads.
What the method area contains are elements that are always unique throughout the entire program, including all classes and static variables.
3. Initializing objectsDog dog = new Dog()
- 1. When creating a Dog object for the first time, first execute steps 1 and 2 above.
- 2. Allocate enough storage space on the heap for the Dog object. All properties and methods are set to default values (numbers to 0, characters to null, booleans to false, and all references to null).
- 3. Execute the constructor and check whether there is a parent class. If there is a parent class, the parent class constructor will be called first. Here, assuming Dog has no parent class, execute the assignment of default value fields, i.e., the method initialization actions.
- 4. Execute the constructor.
Initialization when there is a parent class
Assume:Dog extends Animal
1. Execute the first step to find the Dog.class file. During loading, it is discovered that it has a base class (via the extends keyword), so first execute steps 1 and 2 for the Animal class to load its static variables and methods. After loading is complete, then load the static variables and methods of the subclass Dog.
If the Animal class also has a parent class, and so on, the ultimate base class is called the root base class.
Note:Because the subclass's static initialization may depend on the parent class's static resources, the parent class's static resources must be loaded first.
2. Next, we want to create a new Dog object. First allocate storage space for the Dog object -> go to Dog's constructor -> create default properties. Here, the first line inside its constructor has an implicit super(), i.e., the parent class constructor, so at this point it will jump to the parent class Animal's constructor.
Java will help us complete the supplement to the constructor. Dog's actual implicit constructor is as follows:Dog() {
//创建默认的属性和方法
//调用父类的构造函数super()(可显式写出)
//对默认属性和方法分别进行赋值和初始化
}
3. Before executing its constructor, the parent class Animal also allocates storage space -> goes to its constructor -> creates default properties -> discovers "wow, I no longer have a parent class." At this point, it assigns values to its default properties and initializes methods.
4. Then execute the remaining part of the constructor. After it ends, jump back to the subclass Dog's constructor.
5. The subclass Dog assigns values to and initializes its default properties and methods respectively, then completes the remaining part of the constructor.
1. Why must the parent class Animal's constructor be executed before continuing with the subclass Dog's property and method assignment?
Because the initialization of the subclass Dog's non-static variables and methods may use the parent class Animal's properties or methods, the subclass should not assign values to its default properties and methods after constructing them. Instead, it should jump to the parent class's constructor to complete the construction of the parent class object, and only then initialize its own properties and methods.
This is also why, when the subclass constructor explicitly calls the parent class constructor super(), it must be forced to be written on the first line. The program needs to jump to the parent class constructor to complete the construction of the parent class object before it can execute the remaining part of the subclass constructor.
。2. 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 all to ensure that the things needed for subsequent resource initialization have already been initialized earlier. Amazing, kudos to the fathers of Java.
Enough talking, let's look at an example.
Note here that the main function is also a static resource. Executing the Dog class's main function is calling Dog's static resources.
Example
The test output result is:
static Animal.x1 initialized static Dog.x2 initialized Dog constructor i = 9, j = 0 Dog.k initialized k = 47 j = 39 Main Left
Original article URL: https://blog.csdn.net/qq_25665807/article/details/74452181