17

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Phil Meyer
  • 181
  • 1
  • 1
  • 4
  • 2
    The interesting thing here is that, since these aren't constructors, the call to `super()` is trying to call the constructor of the parent class from inside a method in the child. The error message says "Its not the first line in the child's constructor." That is true but not terribly helpful. Maybe a message like "Call to super not in constructor" would be nice. – Lee Meador May 06 '13 at 21:24
  • I was definitely not expecting to have answers this quickly. Of course, it was a simple fix that I was too blind to see. But still, thank you to all. – Phil Meyer May 06 '13 at 21:25

6 Answers6

38

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • 2
    Note that while `customer` (with a small C) is a valid class name, it is frowned upon. By convention, class names must start with a capital letter (`Customer`) while method names start with a small letter. – ADTC Dec 18 '14 at 09:03
8
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){
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3
public void CheckingAccountCustomer(int a)

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

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
2

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
2

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.

javadev
  • 1,639
  • 2
  • 17
  • 35
0

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

}
KANJICODER
  • 3,611
  • 30
  • 17