How is the constructor executed?

I make some changes from the lecture slides, and he says that the constructor is executed as follows:

  • If the constructor starts from this, recursively execute the specified constructor, and then go to step 4.

  • Invokes the explicitly or implicitly specified superclass constructor (unless that class is java.lang.Object ).

  • Initialize the fields of the object in the order in which they were declared in this class.

  • Run the rest of the body of this constructor.

I donโ€™t understand that a constructor can never โ€œstartโ€ from this, because even if it does not form a hierarchy / class relation, the super () function is added by default.

How does this fit into the description above?

+6
java initialization jvm
source share
4 answers

The constructor (for every class except java.lang.Object) must start with "super ()" to call its superclass constructor or "this ()" to call another constructor of the same class. If you do not include any of them in your constructor, the compiler will insert a call to super (). It is normal for a constructor to start by calling another constructor in the same class if the class constructor is ultimately called, calling the constructor of the superclass.

+8
source share

I do not think that you are right, or I do not understand this problem. From the Java Language Spec :

If the constructor body does not start with an explicit constructor call, and the constructor declared is not part of the original class Object, then the body constructor is implicitly accepted by the compiler to start with the superclass, call the constructor "super ();", the constructor call its direct superclass that does not accept Arguments

How such a constructor can start with this(...) , which calls another constructor of the same class. Only when a constructor is called that does not start with this (...) or super (...), super () is called automatically.

I would say that after creating the object, super (...) was called (if the class is not java.lang.Object).

+2
source share

here is a more or less practical example of using this(...)

 public class Car { private final int numberOfDoors; public Car(int doors) { numberOfDoors = doors; } public Car() { this(4); // default is 4 doors, calls the constructor above } } 
+2
source share

the first statement in the body of the constructor must be this or super, if there is no defalt compiler in it, the super () keyword is saved (without an argument). therefore, the constructor body is executed as follows:

  • Execution will happen based on this or super keyword, then

  • he will execute the whole IIB (as a top-down approach), then

  • it will execute all the records saved by the programmer (e.g. sop, initilization)

 Class A{ A() { this(10); //or super,....... execution statement 1 // executing IIB's, execution statement 2 System.out.println("from A()"); // execution statement 3 } A(int i) { System.out.println("from A(int)"); } { System.out.println("from IIB-1"); } public static void main(String[] args){ A a = new A(); //calling constructor A() System.out.println("from main"); } { System.out.println("from IIB-2"); } } 

Output:

 from IIB-1 from IIB-2 from A(int) from A() from main 
0
source share

All Articles