Why Do This() and Super() Have to Be the First Statement in a Constructor

Call to super must be first statement in the constructor, but it is

This code

public void customer(String n, int p, double b){

is not a constructor. Constructors don't have return types, e.g. void. Assuming your class name is customer:

public customer(String n, int p, double b){

This applies to CheckingAccountCustomer too.

call to super() must be the first statement in constructor body

Java enforces that the call to super (explicit or not) must be the first statement in the constructor. This is to prevent the subclass part of the object being initialized prior to the superclass part of the object being initialized.

In your case, you don't do anything but local "trivial computation", so all things considered, it would be okay. However, Java's compiler doesn't go to the level of determining whether statements before a call to super actually do any initialization. It just disallows all statements before super().

Why can't this() and super() both be used together in a constructor?

this(...) will call another constructor in the same class whereas super() will call a super constructor. If there is no super() in a constructor the compiler will add one implicitly.

Thus if both were allowed you could end up calling the super constructor twice.

Example (don't look for a sense in the parameters):

class A {
public A() {
this( false );
}

public A(boolean someFlag) {
}
}

class B extends A {
public B() {
super();
}

public B( boolean someFlag ) {
super( someFlag );
}

public B ( int someNumber ) {
this(); //
}
}

Now, if you call new B(5) the following constructors are invoked:

     this( false);
A() ---------------> A(false)
^
|
| super();
|
| this();
B() <--------------- B(5) <--- you start here

Update:

If you were able to use this() and super() you could end up with something like this:

(Attention: this is meant to show what could go wrong, if you were allowed to do that - which you fortunately aren't)

     this( false);
A() ---------------> A(false)
^ ^
| |
| super(); | super( true ); <--- Problem: should the parameter be true or false?
| |
| this(); |
B() <--------------- B(5) <--- you start here

As you can see, you'd run into a problem where the A(boolean) constructor could be invoked with different parameters and you'd now have to somehow decide which should be used. Additionally the other constructors (A() and B()) could contain code that now might not get called correctly (i.e. out of order etc.) since the call to super( true ) would circumvent them while this() wouldn't.

Constructor call must be the first statement in a constructor

As you know, this works:

A() {
this(1);
System.out.println("1");
}

Why? because it's a rule of the language, present in the Java Language Specification: a call to another constructor in the same class (the this(...) part) or to a constructor in the super class (using super(...)) must go in the first line. It's a way to ensure that the parent's state is initialized before initializing the current object.

For more information, take a look at this post, it explains in detail the situation.



Related Topics



Leave a reply



Submit