Is it true that calling this () inside the constructor implicitly calls super ()?

I tried to understand

why can't this () and super () be used together?

I read many of the related discussions here about stackoverflow and understood a lot. But I still have one confusion.

calling this () inside the constructor implicitly calls super ()

consider this code ..

class Top { int z; Top() { System.out.println("Top default constructor"); } } class A extends Top { int x; A(int num) { x=num; System.out.println("A parameterized constructor"); } A() { this(5); System.out.println("A default constructor"); } public static void main(String arg[]) { new A(); } } 

and OUTPUT:

Top default constructor

Parameterized Constructor

Default constructor

I did not expect the first line in the output of "Top default constructor", since there is no super () call, implicit or explicit.

So there is perhaps something that I misunderstood. Explain, please.

+5
source share
4 answers

Calling this() inside the constructor calls another constructor of the same class. Another constructor will call the super() constructor (implicitly or explicitly), so you cannot call both this() and super() in the same constructor, as this will cause two super() constructors to be called.

Note that whenever I write this() or super() , I do not have to refer to the constructor without parameters (except for the implicit call to super() , which always refers to the constructor without parameters, the super class, as Joeblade commented) . Both calls can have parameters.

In your code example, the constructor A() calls the constructor A(int) constructor (which does this(5) ), which implicitly calls the constructor Top() (less parameter).

+2
source

Is it true that calling this () inside the constructor implicitly calls super ()?

Calling this() in the constructor will invoke the zero-args constructor for this class. If the zero-args constructor for this class does not have an explicit call to super(...) , then yes, there will be an implicit call to the zero-args constructor super() . If the zero-args constructor in your class has an explicit call to some other super signature, then of course this is done instead.

This is true for designers in general. In your class A , since your constructor A(int) does not have a call for this() or super() , implicit super() is executed.

I did not expect the first line in the output of "Top default constructor", since there is no call to super() , implicit or explicit.

Yes, there is - implicit. :-)

The basic rule is this: some base class constructor must be run before the code in the derived class. Therefore, this(...) or super(...) calls must be the first in the constructor. If the constructor does not have an explicit call to super(...) , there is always an implicit call to super() (with no arguments).

+8
source

this(5) calls A(int num) , which calls super() implicitly.

+2
source

This is explained in the "Java Language Specification" section on "Creating Instances of a New Class" with an emphasis on the superclass constructor in bold:

Before the result is a reference to the newly created object, the specified constructor is processed to initialize the new object using the following procedure:

  • Assign constructor arguments to the newly created parameter variables for this constructor call.

  • If this constructor begins by explicitly calling the constructor (Β§8.8.7.1) of another constructor in the same class (using this), then evaluate the arguments and process the constructor call recursively using these five steps. If the constructor call terminates abruptly, then this procedure terminates abruptly for the same reason; otherwise go to step 5.

  • This constructor does not start by explicitly calling the constructor of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will start by explicitly or implicitly invoking the superclass constructor (using super). Evaluate the arguments and handle the call to the superclass constructor recursively using these five steps . If the constructor call terminates abruptly, then this procedure terminates abruptly for the same reason. Otherwise, go to step 4.

  • Run instance initializers and instance variable initializers for this class, assigning the initializer values ​​of the instance variable to the corresponding instance variables in the order from left to right, in which they are displayed in text form in the source code for the class. If the execution of any of these initializers results in an exception, then no new initializers are processed, and this procedure terminates abruptly with the same exception. Otherwise, go to step 5.

  • Run the rest of the body of this constructor. If this execution completes abruptly, then this procedure terminates abruptly for the same reason. Otherwise, this procedure is performed normally.

So, in your case, when the constructor A(int num) is called using this(5) , step 3 indicates that it implicitly calls the constructor of the superclass, applying the same procedure recursively.

Note that you can follow the procedure by debugging the sequence of constructor calls.

+1
source

Source: https://habr.com/ru/post/1212101/


All Articles