This () constructor is not needed?

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

In constructor 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 the class U1 and do in class U everything that has been done in U1 so far ... So, now I will not need to call the constructor of the superclass, since the class U does not have any superclass ...

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

Here's how my U constructor would look like this:

 public U(Plate plate, int order) { this(plate, order); ... } 
+4
source share
5 answers

This is not necessary, and I expect this to lead to a stack overflow, because you are calling the constructor itself inside the constructor.

+8
source

This will result in a compilation error. JLS Section 8.8.7 states:

"This is a compile-time error for a constructor to directly or indirectly call itself through a series of one or more explicit constructor calls involving this ."

In this case, the constructor calls directly.

+7
source

in the sample, as shown below, we get the error: call the recursive constructor,

 class TestConstruct{ public TestConstruct(){ this(); System.out.println("constructor of Test class"); }//end of constructor }//end of class TestConstruct public class AppConstruct{ public static void main(String[] a){ Test t = new Test(); }//end of main }//end of AppConstruct 
+1
source

This is not to be missed. You must omit it, otherwise it will act as an infinite recursive call.

0
source

We call super () in the constructor to initialize the instance variables inherited from the superclass, so there is no need to call super () if there is no superclass - the java.lang.Object exception. If there are multi-constructors, we can do this () to call other constructors, but do not call the constructor itself, which causes the bad things that previous people talked about.

0
source

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


All Articles