Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do computation before super() in the constructor?

Tags:

java

Given that I have a class Base that has a single argument constructor with a TextBox object as it's argument. If I have a class Simple of the following form:

public class Simple extends Base {   public Simple(){     TextBox t = new TextBox();     super(t);     //wouldn't it be nice if I could do things with t down here?   } } 

I will get a error telling me that the call to super must be the first call in a constructor. However, oddly enough, I can do this.

public class Simple extends Base {   public Simple(){     super(new TextBox());   } } 

Why is it that this is permited, but the first example is not? I can understand needing to setup the subclass first, and perhaps not allowing object variables to be instantiated before the super-constructor is called. But t is clearly a method (local) variable, so why not allow it?

Is there a way to get around this limitation? Is there a good and safe way to hold variables to things you might construct BEFORE calling super but AFTER you have entered the constructor? Or, more generically, allowing for computation to be done before super is actually called, but within the constructor?

Thank you.

like image 973
Stephen Cagle Avatar asked Feb 20 '10 20:02

Stephen Cagle


People also ask

Why must super () or this () Be the first statement in a constructor?

The Eclipse compiler says, Constructor call must be the first statement in a constructor . So, it is not stopping you from executing logic before the call to super() . It is just stopping you from executing logic that you can't fit into a single expression. There are similar rules for calling this() .

Can you use this () and super () both in a constructor?

“this()” and “super()” cannot be used inside the same constructor, as both cannot be executed at once (both cannot be the first statement). “this” can be passed as an argument in the method and constructor calls.

Is it necessary to call super () inside a constructor?

However, using super() is not compulsory. Even if super() is not used in the subclass constructor, the compiler implicitly calls the default constructor of the superclass.

Where super () can be used within a constructor?

super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.


1 Answers

Yes, there is a workaround for your simple case. You can create a private constructor that takes TextBox as an argument and call that from your public constructor.

public class Simple extends Base {     private Simple(TextBox t) {         super(t);         // continue doing stuff with t here     }      public Simple() {         this(new TextBox());     } } 

For more complicated stuff, you need to use a factory or a static factory method.

like image 125
waxwing Avatar answered Sep 20 '22 15:09

waxwing