Java object-oriented -- object initialization and java object-oriented Initialization
Overload means that multiple methods have the same name in a class, and the parameter list (the type, number, and order of parameters) is different. The type and modifier of the returned value are irrelevant to the overload. Solve the problem of using different names for behaviors with the same meaning.
The biggest role of the constructor is to perform initialization when creating an object.
The default constructor, also known as the parameter-free constructor, is used to create a default object. If the class does not have any constructor, the compiler automatically creates a default constructor. If you have already defined Any constructor, the compiler will not automatically create a constructor.
All instances share a method. the compiler will pass the reference of the current object as the first parameter into the method to distinguish different calling objects.
This: used inside a method. this indicates a reference to the object that calls the method.The biggest function is to allow a method in the class to access another method or attribute of the class.
The static method does not use this method, so static members cannot access non-static members.
Static Keyword: the modified member indicates that it belongs to a common class, which can be accessed through a class or an instance.
You do not need to use this to access members of the same class within the method.
If the parameter variables and member variables have the same name, you can use this. field = field to differentiate them.
Call the constructor In the constructor: this can refer to other constructor methods that are overloaded to reduce code duplication. Note that this (arg) must be the first code in the constructor.
public class Teacher { private String name; private int age; public Teacher(String name,int age ){ this(name); this.age=age; } public Teacher(String name){ this.name = name; }}
Initialization sequence of static data, member variables, and constructors
If no inheritance exists:
Suppose there is a class named Dog. the creation process is as follows:
1. when a Dog-type object is created for the first time (the constructor can be regarded as a static method), or when the static method/static field of the Dog class is accessed for the first time, the Java interpreter must look for the class path, to locate the compilation code of the Dog class (in the Dog. class file ).
2. Load the Dog. class (this creates a Class Object) and perform static initialization. Therefore, static Initialization is only performed once when the Class object is loaded for the first time. Static variables and static initial block are initialized in the defined order.
3. When new Dog () is used to create an object, sufficient storage space will be allocated to the Dog object on the heap.
4. this bucket is cleared, which automatically sets all the basic types of data in Dog to the default value (0 for numbers and the same for Boolean and numeric types ), the reference is set to null. (Default initialization)
5. Execute all initialization actions that appear in the member variable definition. (Display Initialization is performed in the defined sequence,Including member variables and initialization Blocks)
6. Execute the constructor.
7. After the object is constructed, assign the address to the reference type variable.
When inheritance exists:
Suppose there is a class named Golden that inherits from Dog and Animal. the initialization process is as follows:
1. When a Golden-type object is created for the first time, the Java loader looks for the compilation code of the Golden class (in the Golden. class file ).
2. the Java loader loads Golden. class file, according to Golden. class (extends keyword) loads its base class Dog. class file, and then according to Dog. class load base class Animal. class file. Then, initialize static data from the base class Animal to the base class Dog to the subclass Golden. (The static data of the subclass is initialized before the member variable of the parent class)
3. When you use new Golden () to create an object, you first create a Golden object on the heap (including the Golden class, the base class Dog, and the base class Animal.All member variables) Allocate enough storage space. Because the sub-class constructor cannot access the private Members of the parent class, you must use the super keyword to initialize these member variables.
4. This bucket is initialized with the default value of binary zero. Set all basic data types in Golden (including their base classes Dog and Animal) to the default value (0 for numbers, the same for Boolean and numeric types ), the reference (including its base class Dog and Animal) is set to null. That is to say, before executing any initialization code, the default initial values have been set for all instance variables.
5. Initialize all member variables in the base class Animal.
6. Run the base root class Animal constructor.
7. Initialize all member variables in the base class Dog.
8. Execute the base class Dog constructor.
9. Execute the initialization action for all member variables in the export class Golden.
10. Execute the export class Golden constructor.
To put it simply, the class file of the class is loaded from the subclass to the base class, and static data is initialized from the base class to the subclass. Allocate a bucket for the object. After the bucket is cleared, initialize the instance from the parent class to the Child class in sequence (including the member variable initialization and two steps to call the constructor ).
When the developer does not define other super statements, Java will automatically insert a call to the parent class constructor In the constructor of the subclass,This call is not a simple execution of the parent class constructor.The specific sequence is as follows (class initialization is omitted ):
1. Enter the constructor of the current class, and call super () (the name will be changed to <init> by the compiler without executing other Code) to directly enter the parent class constructor and Recursion TO java. lang. object Class constructor.
2. Execute the java. lang. Object class initialization in the order of initializing the member variable/initialization block (in the defined order), and then calling the constructor (the super part will not be executed) for initialization.
3. Use Step 2 to initialize the direct subclass of the java. lang. Object Class, and Recursion this process to the current class.
Note:
Objects can only be created using the new constructor () method. Therefore, the compiler provides the default no-argument constructor when the programmer does not write the constructor.Constructors are meant for initializing the members of objects.
When a new object is loaded, the class of the main method is used to run the java application when the static data of the runtime class is used. Static data is initialized when the class is loaded.
The object obtains the space required by all instance variables, including the inherited items.
When an object is created, all constructors of the parent class are executed: during the execution of the constructor, the first thing is to call the constructor of its parent class (the compiler calls the constructor of the parent class with parameters through super or the developer calls the constructor of the parent class by himself) until the chain returns to the class of the Object, that is, "constructor chain ".
During object creation, subclass objects may access data inherited from the parent class. Therefore, super () must be the first statement of the constructor, the parent class constructor must end before the subclass constructor.
This (...) refers to other constructors of the current class. It must also be the first statement in the constructor. Therefore, super () and this () can only be selected.
In addition to saving the instance variables of the class, each object also saves the reference of the actual class information. The memory zone that stores the class information is called the method zone in Java.
A virtual method table creates a table for each class when the class is loaded. This table includes all the dynamically bound methods and addresses of the class objects, including the parent class methods, however, a method has only one record. After the subclass overrides the parent class method, only the subclass is retained.
Class Glyph {void draw () {System. out. println ("Glyph. draw () ") ;}; Glyph () {System. out. println ("Glyph () before draw ()"); draw (); // calls the rewritten child type method System. out. println ("Glyph () after draw ()") ;}} class RoundGlyph extends Glyph {private int radius = 1; RoundGlyph (int r) {radius = r; System. out. println ("RoundGlyph. roundGlyph (), radius = "+ radius);} void draw () {System. out. println ("RoundGlyph. draw (), radius = "+ radius) ;}} public class PolyConstructors {public static void main (String [] args) {new RoundGlyph (5 );}} /* Glyph () before draw () RoundGlyph. draw (), radius = 0 Glyph () after draw () RoundGlyph. roundGlyph (), radius = 5 */