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

Java

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

I had the same problem with computation before super call. Sometimes you want to check some conditions before calling super(). For example, you have a class that uses a lot of resources when created. the sub-class wants some extra data and might want to check them first, before calling the super-constructor. There is a simple way around this problem. might look a bit strange, but it works well:

Use a private static method inside your class that returns the argument of the super-constructor and make your checks inside:

public class Simple extends Base {
  public Simple(){
    super(createTextBox());
  }

  private static TextBox createTextBox() {
    TextBox t = new TextBox();
    t.doSomething();
    // ... or more
    return t;
  }
}

Solution 3 - Java

It is required by the language in order to ensure that the superclass is reliably constructed first. In particular, "If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass."

In your example, the superclass may rely on the state of t at construction time. You can always ask for a copy later.

There's an extensive discussion here and here.

Solution 4 - Java

You can define a static supplier lambda which can contain more complicated logic.

public class MyClass {
    
    private static Supplier<MyType> myTypeSupplier = () -> {
        return new MyType();
    };
    
    public MyClass() {
        super(clientConfig, myTypeSupplier.get());
    }
}

Solution 5 - Java

The reason why the second example is allowed but not the first is most likely to keep the language tidy and not introduce strange rules.

Allowing any code to run before super has been called would be dangerous since you might mess with things that should have been initialized but still haven't been. Basically, I guess you can do quite a lot of things in the call to super itself (e.g. call a static method for calculating some stuff that needs to go to the constructor), but you'll never be able to use anything from the not-yet-completely-constructed object which is a good thing.

Solution 6 - Java

That's how Java works :-) There are technical reasons why it was chosen this way. It might indeed be odd that you can not do computations on locals before calling super, but in Java the object must first be allocated and thus it needs to go all the way up to Object so that all fields are correctly initialized before your could accidently moddify them.

In your case there is most of the time a getter that allows you to access the parameter your gave to super(). So you would use this:

super( new TextBox() );
final TextBox box = getWidget();
... do your thing...

Solution 7 - Java

This is my solution that allows to create additional object, modify it without creating extra classes, fields, methods etc.

class TextBox {
    
}

class Base {

    public Base(TextBox textBox) {
        
    }
}

public class Simple extends Base {

    public Simple() {
        super(((Supplier<TextBox>) () -> {
            var textBox = new TextBox();
            //some logic with text box
            return textBox;        
        }).get());
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephen CagleView Question on Stackoverflow
Solution 1 - JavawaxwingView Answer on Stackoverflow
Solution 2 - JavaBenView Answer on Stackoverflow
Solution 3 - JavatrashgodView Answer on Stackoverflow
Solution 4 - JavaloweryjkView Answer on Stackoverflow
Solution 5 - JavaCodingInsomniaView Answer on Stackoverflow
Solution 6 - JavaDavid NoulsView Answer on Stackoverflow
Solution 7 - JavaPavel_KView Answer on Stackoverflow