Why can't we have this() and super() together in Java? [duplicate]

This question already has answers here:

this and super must be the first line in a constructor.


Language spec

8.8.7. Constructor Body

The first statement of a constructor body may be an explicit invocation of another constructor of the same class or of the direct superclass (§


this() calls another constructor in the same class.

super() calls a super constructor.If no super() is explicitly written,the compiler will add one implicitly. Hence, you will end up calling super() twice.

So, both are not allowed.


In context of your code : remember, super() should always be the first line in a constructor.


Upon further reflection my answer as it was below is basically correct but lacking some nuance. Essentially, you can call a super constructor once. This is to ensure your super class is only constructed once. This means that the first line of a given constructor can be a call to another constructor in the current class or a call to a constructor in the super class. This also means that you can only call another constructor once in any given constructor; you must choose to call one in the current or super class. This ensures that all super classes will be fully constructed before the current object is.

Old explanation:

The fundamental reason is that all super classes must be constructed before the subclass can be. To this end, Java will implicitly call super() if no such invocation exist on the first line of a constructor. The only way to override this behavior is to explicitly call a different constructor in your super class. Basically, Java must create your hierarchy before you can be created.

Putting your constructor first violates this requirement and therefore is illegal.


Reading here, it seems modelling a custom exception class using a generalisation is common place. What it doesn't mention is how I can model an association with a class that could potentially throw ...

I'm really confused with the concept of static vs instance methods. I have created a BMR calculator. I've seperated the GUI from the calculations using different classes. public class Calculations { ...

I need to call some functions generated by some libs. I will need to call fucntion1, function2, ..., function10 one by one. Instead of writing them all out on the code, is there any clever way to code ...

this is the snippet that produces an error saying : ')' expected ';' expected not a statement cannot find symbol symbol : variable ActionEvent The snippet : private void jMenuItem5ActionPerformed(...