Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I keep getting an error saying that "call to super must be the first statement in the constructor".

The problem is that it is the first statement in my constructor.

public void CheckingAccountCustomer(int a){
    super(n, p, b);
    accountNo = a;
}

And here is my superclass for this as well.

public void customer(String n, int p, double b){
    name = n;
    pin = p;
    balance = b;
}

What am I doing wrong here?

like image 434
Phil Meyer Avatar asked May 06 '13 21:05

Phil Meyer


People also ask

Why call to this () must be the first statement in constructor?

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.

Which statement is true a super () or this () call must always?

1) A super() or this() call must always be provided explicitly as the first statement in the body of a constructor. 2) If both a subclass and its superclass do not have any declared constructors, the implicit default constructor of the subclass will call super() when run.

Do you have to call super () in constructor?

It is required if the parameterized constructor (a constructor that takes arguments) of the superclass has to be called from the subclass constructor. The parameterized super() must always be the first statement in the body of the constructor of the subclass, otherwise, we get a compilation error.

What is the role of super statement in constructor?

It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.


5 Answers

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.

like image 147
rgettman Avatar answered Oct 06 '22 05:10

rgettman


public void CheckingAccountCustomer(int a){

That's not a constructor since it states it has a void return type. It's just a method of the same name as the class. Get rid of the return type.

public CheckingAccountCustomer(int a){
like image 32
Hovercraft Full Of Eels Avatar answered Oct 06 '22 05:10

Hovercraft Full Of Eels


public void CheckingAccountCustomer(int a)

This is a method not a constructor, since it has a return type.

like image 40
Eng.Fouad Avatar answered Oct 06 '22 04:10

Eng.Fouad


The constructor is used to create an instance of that Class, so it make no sense if it will let the user to change the return type (it can be dangerous too). That's why constructors has no return type.

As others have already answered, remove the return type and it'll become a constructor.

like image 42
Maroun Avatar answered Oct 06 '22 05:10

Maroun


Constructors never return something (either void or Object type).

public void CheckingAccountCustomer(int a){
    super(n, p, b);
    accountNo = a;
}

thus is not a constructor.

like image 35
javadev Avatar answered Oct 06 '22 03:10

javadev