Incompatible return type error java

后端 未结 5 1479
礼貌的吻别
礼貌的吻别 2021-01-17 06:52
class BankAccount {
    private String firstname;
    private String lastname;
    private int ssn;
    private int accountnumber = 0;
    private double accountbala         


        
5条回答
  •  生来不讨喜
    2021-01-17 07:28

    A void method cannot return someting. And a method which have a return type must return the type specified in method signature.

    Error 1

    For example look this method

     public void setAccountNumber(int accountnumber) {
       return accountnumber;
       }
    

    Yo cannot return from a void method.

    That should be

      public void setAccountNumber(int accountnumber) {
               this.accountnumber =accountnumber;
           }
    

    Same goes for remaining methods too.

    Error 2

    public void deposit(double amount) {
            return this.accountbalance = this.accountbalance + amount;
        }
    

    that return statement is syntactically wrong. You cannot return as it is void. That should be

      public void deposit(double amount) {
                this.accountbalance = this.accountbalance + amount;
            }
    

提交回复
热议问题