Member variables and constructor execution order

Creating an object in the Java virtual machine consists of the following steps.
(1) Allocate memory to the object.
(2) Automatically initialize the instance variables of the object to the default values ​​of their variable types.
(3) Initialize the object and assign the correct initial value to the instance variable.
  For the third step above, the Java virtual machine can use three methods to initialize the object, and which initialization method is used depends on the method of creating the object.
(1) If the object is created by the clone() method, the Java virtual machine copies the value of the instance variable of the original cloned object to the new object.
(2) If the object is created by the readObject() method of the ObjectInputStream class, the Java virtual machine initializes those non-transient instance variables by reading the serialized data from the input stream.
(3) In other cases, if the instance variable is explicitly initialized at the time of declaration, then the initialization value is assigned to the instance variable, and then the constructor is executed. This is the most common way to initialize objects.
 
1. The order of initialization is: first static objects (if they have not been initialized by the previous object creation process), then non-static objects. Inside a class, the order of variable definitions determines the order of initialization, and even if variable definitions are scattered among method definitions, they will still be initialized before any methods (including constructors) are called.

Summarize the process of object creation:

(1) When the object is first created, when the static method/static field in the class is accessed for the first time, the java interpreter must first look up the classpath to locate the .class file; (2) Then load the .class (this will create a class object), all actions related to static initialization are performed. Therefore, static initialization is only done once when the Class object is first loaded. (3) When creating an object with new XX(), first allocate enough storage space for the object on the heap. (4) This storage space will be cleared to 0, which automatically sets all basic type data in the object to the default value (0 for numbers, and the same for Boolean and character types), and The reference is set to null. (5) Execute all initialization actions (initialization of non-static objects) that appear in field definitions. (6) Execute the constructor.

1. Initialization of objects
(1) Initialization of non-static objects 
  When an object is created, all data members of the class where the object is located will be initialized first. Basic type: int, initialized to 0. If objects: These objects are initialized sequentially. 
※After all class members are initialized, the constructor of this class is called to create an object. The role of the constructor is to initialize. 
(2) 
  The static variable of the main class in the initializer of the static object will be initialized before the main method is executed. Not only are all static variables in a class initialized when an object is first created, but also when a static object of a class (note that no such object is created at this time) is accessed for the first time, all static variables are also in the class as they are in the class sequence initialization. 
2. During inheritance, the initialization process of the object 
(1) The superclass of the main class initializes the static members in order from high to low, regardless of whether the static members are private or not. 
(2) Initialization of static members of the main class. 
(3) The superclass of the main class calls the default constructor from high to low. Note that the non-static members of the superclass are initialized before calling the default constructor of each superclass. 
(4) Initialization of non-static members of the main class. 

(5) Call the constructor of the main class.  


Between child class and parent class:

// The initialization sequence of each object is as follows:  
             // ①Subclass static member variable  
             // ②Subclass static constructor  
             // ③Subclass instance member variable  
             // ④Parent class static member variable  
             // ⑤Parent class static constructor  
             / / ⑥ Parent class instance member variable  
             // ⑦ Parent class constructor  
             // ⑧ Sub class constructor  
Member variables take precedence over constructors

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691594&siteId=291194637