http://jzinfo.iteye.com/blog/620045
Specific question: When creating an object in Java using new, the detailed procedure is what.
Each Java code in Java needs to be able to execute a bytecode file that is first compiled into a class. The class loader is then loaded into the JVM before it can be executed.
1. All classes are dynamically loaded into the JVM when they are used for the first time. When the first time an object of type dog is created, or a static method of the dog class, when the static domain is first accessed, the Java interpreter looks for classpath and navigates to the Dog.class file
2. Load the Dog.class file to generate a class type object.
All related static initialization actions are executed, static code blocks, static member properties. And this initialization action is done only once when the class object is first loaded.
3. When creating an object with new dog (), the JVM first allocates enough storage space on the heap to the dog object
4. Empty storage space, automatically set all the basic type data in the dog object to the default value, the object reference is set to NULL
5. Perform all initialization operations at the field definition
6. Call the constructor method. (No inheritance)
The above is the execution of the case without inheritance, if there is multiple inheritance in the case?
After the Java interpreter finds the Dogexamp.class file in the classpath, it locates Jiingbadog.class and Dog.class according to the inheritance rules. Each loaded into the corresponding class object, at this time according to the above process, followed by the static initialization of the action.
Static initialization is performed first in the dog class, causing the Dogprofile object to be instantiated and the first line to be output when the static property profile is initialized. The static code block is then executed, resulting in the second line being output.
Next, the subclass Jingbadog performs a static initialization action, similarly initializing the profile static property, resulting in instantiating the Dogprofile object, outputting the third line, and, similarly, initializing the static member in Dogexamp, outputting line 4th.
After the static initialization, the constructor of the base class dog is executed, and the 5th row is output, according to the constructor method of the class's inheritance.
It then executes the Jingbadog construction method, outputting line 6th.
The final execution of its own construction method outputs line 7th.
Based on the above analysis, we conclude that:
1. When you first create an object of a class (that is, the constructor method is called, the constructor is implicitly static), or if the static method of the class is invoked, the Java interpreter will look for the class file of the classes object when the static member property is referenced
2. When you generate a class object after loading the class file, the static initialization of all actions is performed. (includes static members of the parent class and static blocks of code)
3. Execute the initialization action of the class definition at the field
4. Recursive call constructor Execution construction method
....
The object creation process in Java