Java object initialization and java object initialization
Object A creation process:
1. the constructor is actually A static method. Therefore, when the static method/static domain of the object A or Class A is accessed for the first time, the Java interpreter looks for the class path
A. class file.
2. Load A. class. All actions related to static initialization are executed (). Therefore, static Initialization is only performed once when the Class object is loaded for the first time.
3. When creating an object with new A (), allocate enough storage space for object A on the heap.
4. the bucket is cleared, so that all basic data of object A is automatically set to the default value, and the reference is set to null.
5. Execute all initialization tasks that appear in the field definition.
6. Execute the constructor.
Attributes, methods, constructor methods, and free blocks are all members of the class. When creating class objects, the execution sequence of the members of the class is as follows:
1. The static members and static initialization of the parent class are fast and executed in sequence when the code appears.
2. Subclass static members and static initialization blocks are executed in order of code appearance.
3. The instance members and instance initialization blocks of the parent class are executed in order of code appearance.
4. constructor of the parent class (if it is not explicitly specified to call the constructor of a parent class, the default constructor of the parent class will be called by default. If there is no default constructor, the compiler will
Error)
5. Subclass instance members and instance initialization blocks are executed in order of code appearance.
6. constructor of subclass.
An effective criterion for writing constructor: (Java programming ideology Fourth Edition Chinese P163)
"Use the simplest possible method to make the object normal. If possible, avoid calling other methods." The only method in the constructor that can be safely called is the final method in the base class.