Phil Meyer
Phil Meyer

Reputation: 191

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?

Upvotes: 18

Views: 27289

Answers (6)

KANJICODER
KANJICODER

Reputation: 3885

Make sure your "constructor" is really a constructor.

With the monumentious amounts of boilerplate bullshit in Java it is a wonder to me why constructors don't have a "constructor" keyword. I mean, Java doesn't have a problem with being overly verbose, yet likes to be terse in places where being verbose would be a good idea.

//:WRONG://

@Service
public class      EmployeeServiceImpl 
       implements EmployeeServiceInterface
{

    private EmployeeRepository employeeRepository ;

    /** Constructor **/

    public EmployeeServiceImpl saveEmployee(
        EmployeeRepository employeeRepository
    ){
        super(); //:No blank line allowed? ://
        this.employeeRepository = employeeRepository ;

    }

}

//:FIXED://

@Service
public class      EmployeeServiceImpl 
       implements EmployeeServiceInterface
{

    private EmployeeRepository employeeRepository ;

    /** Constructor **/

    public EmployeeServiceImpl(
        EmployeeRepository employeeRepository
    ){
        super(); //:No blank line allowed? ://
        this.employeeRepository = employeeRepository ;

    }
}

Because java allows overloading, it didn't pick up on this mistake. Because right below my constructor was this:

@Override
public EmployeeDAO saveEmployee( EmployeeDAO employee ){

        return employeeRepository.save( employee ) ;

}

Upvotes: 0

javadev
javadev

Reputation: 1669

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.

Upvotes: 2

Maroun
Maroun

Reputation: 95958

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.

Upvotes: 2

Eng.Fouad
Eng.Fouad

Reputation: 117587

public void CheckingAccountCustomer(int a)

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

Upvotes: 3

rgettman
rgettman

Reputation: 178263

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.

Upvotes: 39

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

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){

Upvotes: 8

Related Questions