Execution order of subclass object instantiation
In order to get the execution order of subclass object instantiation, a parent class AddClass and its subclass SonAddClass are defined. Three constructors are defined in the subclass and parent class respectively. Now set breakpoints for the new statement of the subclass through the debug function of the IDE to track the execution order of subclass object instantiation.
class AddClass { private int x=0,y=0,z=0; AddClass (int x) { this.x=x; } AddClass (int x,int y) { this(x); this.y=y; } AddClass (int x,int y,int z) { this(x,y); this.z=z; } public int add() { return x+y+z; } } public class SonAddClass extends AddClass{ int a=0,b=0,c=0; SonAddClass (int x) { super(x); a=x+7; } SonAddClass (int x,int y){ super(x,y); a=x+5; b=y+5; } SonAddClass (int x,int y,int z){ super(x,y,z); a=x+4; b=y+4; c=z+4; } public int add() { System.out.println("super:x+y+z="+super.add()); return a+b+c; } public static void main(String[] args){ SonAddClass p1=new SonAddClass (2,3,5); SonAddClass p2=new SonAddClass (10,20); SonAddClass p3=new SonAddClass (1); System.out.println("a+b+c="+p1.add()); System.out.println("a+b="+p2.add()); System.out.println("a="+p3.add()); } }
On the first
SonAddClass p1=new SonAddClass (2,3,5);
After setting the breakpoint, the results are shown in the following figure:
After the step into method of breakpoint debugging is adopted, the cursor moves to
SonAddClass (int x,int y,int z){ super(x,y,z); a=x+4; b=y+4; c=z+4; }//At this time, x: 2; y: 3; z: 5; a: 0; b: 0; c: 0;
After the step into method of breakpoint debugging is adopted again, the cursor moves to
AddClass (int x,int y,int z) { this(x,y); this.z=z; }//Move the cursor to this(x,y); At this time, X: 2; y: 3; z: 5;
After the step into method of breakpoint debugging is adopted again, the cursor moves to
AddClass (int x,int y) { this(x); this.y=y; }//Move the cursor to this(x); At this time, X: 2; y: 3
After the step into method of breakpoint debugging is adopted again, the cursor moves to
AddClass (int x) { this.x=x; }//Move the cursor to this.x=x; At this time, X: 2;
After the step into method of breakpoint debugging is adopted again, the cursor moves to
private int x=0,y=0,z=0;//At this time, x: 2; y: 0; z: 0;
After the step into method of breakpoint debugging is adopted again, the cursor moves down in sequence
class AddClass { private int x=0,y=0,z=0; AddClass (int x) { this.x=x;//The cursor arrives for the first time; At this time, x: 2; } AddClass (int x,int y) { this(x);// this.y=y;//The cursor arrives for the second time; At this time, x: 2; y: 3; } AddClass (int x,int y,int z) { this(x,y); this.z=z;//The cursor arrives for the third time; At this time, x: 2; y: 3; z: 5; } public int add() { return x+y+z; } }
The above procedure shows that when a subclass creates a class object, the order of calling the constructor is:
1. Call the constructor with the same number of parameters in the subclass method first;
2. Call the constructor of the parent class from the bottom up, that is, the this() method;
3. Use the this pointer to assign the attribute of the parent class from top to bottom;
4. when the parent class attribute is assigned, the constructor in the subclass is called and the super() method is used to assign the attributes in the subclass.
After calling the step into method of breakpoint debugging for the first time, and then using the step over method of breakpoint debugging again, the cursor moves to
int a=0,b=0,c=0;//At this time, a: 0; b: 0; c: 0;
After calling the step over method of breakpoint debugging again, the cursor moves to
super(x,y,z); a=x+4; b=y+4; c=z+4;//At this time, x: 2; y: 3; z: 5;a: 6; b: 7; c: 9;
Similarly, when right
SonAddClass p2=new SonAddClass (10,20); SonAddClass p3=new SonAddClass (1);
After setting breakpoints separately, you will get the same result.
Summary:
1. The JVM will read the SonAddClass.class file in the specified path and load it into memory. The parent class AddClass of SonAddClass will be loaded first.
2. Open up space in heap memory and allocate addresses.
3. And initialize the attributes in the object by default in the object space. (not explicitly initialized)
4. Call the corresponding constructor for initialization.
5. In the constructor, the first line will first call the constructor of the parent class for initialization.
6. After the parent class is initialized, the properties of the child class are explicitly initialized.
7. Then perform the specific initialization of the subclass constructor.
8. After initialization, assign the address value to the reference variable.