Why do this() and super() have to be the first statement in a constructor?

653    Asked by LisaHenderson in Java , Asked on May 19, 2021

Java requires that if you call this() or super() in a constructor, it must be the first statement. Why?

For example:

public class MyClass {
    public MyClass(int x) {}
}
public class MySubClass extends MyClass {
    public MySubClass(int a, int b) {
        int c = a + b;
        super(c);  // COMPILE ERROR
    }
}

The Sun compiler says "call to super must be the first statement in the constructor". The Eclipse compiler says "Constructor call must be the first statement in a constructor".

However, you can get around this by re-arranging the code a little bit:

public class MySubClass extends MyClass {
    public MySubClass(int a, int b) {
        super(a + b);  // OK
    }
}

Here is another example:

public class MyClass {
    public MyClass(List list) {}
}
public class MySubClassA extends MyClass {
    public MySubClassA(Object item) {
        // Create a list that contains the item, and pass the list to super
        List list = new ArrayList();
        list.add(item);
        super(list);  // COMPILE ERROR
    }
}
public class MySubClassB extends MyClass {
    public MySubClassB(Object item) {
        // Create a list that contains the item, and pass the list to super
        super(Arrays.asList(new Object[] { item }));  // OK
    }
}

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(). The compiler says "call to this must be the first statement in the constructor".

Why does the compiler have these restrictions? Can you give a code example where, if the compiler did not have this restriction, something bad would happen?

Answered by Diane Carr

The constructor call must be the first statement in a constructor. This will ensure that if you call various methods on the parent class in your constructor, the parent class has already been set up correctly.

What you are striving to do, pass args to the super constructor is quite legal, you just require to construct those args inline as you are taking, or pass them into your constructor and then transfer them to super:

public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                super(myArray);
        }
}
If the compiler did not implement this you could do this:
public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                someMethodOnSuper(); //ERROR super not yet constructed
                super(myArray);
        }
}
In a few circumstances where a parent class has a default constructor the call to super is entered for you undoubtedly by the compiler. Since every class in Java is acquired from Object, the object's constructor must be called anyhow and it must be performed first. The automatic insertion of super() by the compiler acknowledges this. Making super to appear first, enforces that constructor bodies are performed in the correct order which would be: Object -> Parent -> Child -> ChildOfChild -> SoOnSoForth

Hope my answer help you!



Your Answer

Interviews

Parent Categories