a class in which the construction process of an object is constructed
The order is as follows:
1. When the variable is defined, the initial value is obtained first.
2. Call the building block
3. Call the appropriate constructor
A non-final member is not assigned a value anywhere above, then the default value of the corresponding type is obtained.
First look at a class, when initialized, the order in which each construction method is called
Here is an example
Public classmain{ Public Static voidMain (string[] args) {Base b=NewBase (); System.out.println ("The final value of Baseval in base is:" +b.baseval);
/*
baseval=11
Baseval=12
The final value of Baseval in base is: 12
*/
}}classbase{ Public intBaseval = 10;//① { //② Building BlocksBaseval = 11; System.out.println ("Baseval=" +baseval); } PublicBase ()//③{baseval= 12; System.out.println ("Baseval=" +baseval); }}
a process in which an object is constructed in an inheritance chain
1, Java, the constructor of the subclass, if you do not explicitly write with super, the default will call the parent class default constructor.
You can explicitly specify which constructor of the parent class is called through Super. It is important to note that this super call must be the first statement of the subclass constructor
Here is an example.
We do not number the initial value of the field, but only the initialization block and the constructor number.
It can be found that: constructs a subclass, will first call the parent class the entire construction mechanism (note: I did not say: Constructs a subclass, will first constructs a parent class, causes to be explained later), the parent class calls the parent class's parent class the construction mechanism, ... Always take to object. Then, this will trigger a call chain, the process is a bit like recursion, only the high-level function call is finished, the underlying function will be executed.
Public classmain{ Public Static voidMain (string[] args) {Subclass Sub=Newsubclass (); /** Console: *①baseval=11②baseval=12 ③baseval=12 Sub val=91④baseval=12 Sub val=92*/ }}classbase{ Public intBaseval = 10; { Baseval = 11; System.out.println ("①baseval=" +baseval); } PublicBase (){baseval= 12; System.out.println ("②baseval=" +baseval); }}classSubclassextendsbase{ Public intSubval = 90; {Subval= 91; System.out.println ("③baseval=" +baseval+ "Sub val=" +subval); } PublicSubclass () {//Super ()//the subclass constructor implicitly calls the default constructor of the parent class, and of course you can also display the callSubval = 92; System.out.println ("④baseval=" +baseval+ "Sub val=" +subval); }}
Child class, does it generate a parent class object?
I don't know what they say: When you construct a subclass, you first construct a parent class object, and they say the process, exactly how a model.
The key to this question is whether to generate an embedded parent class or a separate parent class.
Obviously, I think that is generated by the embedded parent class, that is, the child class contains the data of the parent class, and the parent class does not exist independently.
Imagine: If new is a subclass object, it will generate a separate parent class, so for a deep inheritance chain. For example, 10 layer, new a top level subclass, will not be in memory generated 10 objects, obviously is not possible, not efficient.
So when the subclass object is constructed, why call the parent class local constructor? That's because the subclass constructs itself by borrowing the constructor class of the parent class. Because the child class contains data for the parent class.
write in the final summary
1. Objects in Java are generated in the heap, and reference variables referencing them are dependent on the situation. For example, if a class object has a string member and the variable name is name, then name is also in the heap. In C + +, if new is used, the object is generated in the heap, if not new, typically in the stack.
2. The member variable of a class is assigned the default value if it is not explicitly initialized. But it's a good idea to specify an initial value.
3. Constructors can be overloaded.
4. If the custom class does not give the constructor, a default parameterless constructor is obtained. If you manually provide a custom constructor, the system does not provide a default parameterless constructor, and if you need a parameterless constructor, you should add it yourself.
5. The final member variable must be initialized on the new object, including the initial value when the final variable is defined, or in the initialization block, or in the constructor.
6. The statement of the static building block executes when the class is first loaded, and when you use the class, the process is complete.
Object constructs in Java