Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor this() unnecessary?

There was a class U1 that was extending class U. Class U was empty...

In the constructor of U1 there was this first line, calling the constructor of the superclass...

public U1(Plate plate, int order)
{
   super(plate, order);
...

}

Now I want to delete class U1 and do in class U whatever was done in U1 so far... So, now I will not need to call constructor of the superclass since class U is not gonna have any superclass...

Is this(plate, order) unnecessary and can I omit it?

This is how my constructor of U is gonna look like:

public U(Plate plate, int order)
    {
       this(plate, order);
    ...

    }
like image 881
JAN ORTS Avatar asked Dec 04 '22 12:12

JAN ORTS


2 Answers

It is unnecessary and I would expect it results in a stack overflow, because you call the constructor itself from within the constructor.

like image 124
Vincent van der Weele Avatar answered Dec 26 '22 11:12

Vincent van der Weele


It will result in a compilation error. The JLS section 8.8.7 says:

"It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this."

In this case, the constructor invokes itself directly.

like image 42
Stephen C Avatar answered Dec 26 '22 11:12

Stephen C