Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Java by (4k points)
edited by

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 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?

1 Answer

0 votes
by (46k points)

The parent class' constructor requires to be called before the subclass' constructor. This will assure you that if you call some methods on the parent class in your constructor, the parent class has already been set up correctly.

What you are attempting to do, pass args to the super constructor is quite legal, you just need 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 circumstances where a parent class has a default constructor the call to super is entered for you automatically by the compiler. Since every class in Java acquires from Object, objects 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

Related questions

0 votes
1 answer
asked Sep 10, 2019 in Java by Nigam (4k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 17, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...