"call to super must be first statement in constructor"

From: Paul Chapman (paul_at_igblan.free-online.co.uk)
Date: 01/30/05


Date: Sun, 30 Jan 2005 11:39:41 -0000

Here is an example code fragment:

abstract class Outer
{
    protected abstract int getValue();
    protected final int value;
    Outer() {
        value = getValue(); }
}

class Subclass extends Outer
{
    protected int getValue() {
        return valueProvider.getValue(); }
    private final SomeClass valueProvider;
    Subclass(SomeClass aValueProvider) {
        valueProvider = aValueProvider;
        super(); }
}

Attempting to compile something like this produces the error message in the
subject line.

Switching the two lines in the Subclass constructor obviously won't work.

Using a separate initialize() method to initialize Outer means that
Outer.value can't benefit from the security of being final.

Is there a way out of this tangle?

Cheers, Paul



Relevant Pages