Search code examples
javaclassthis

Variable that I point to using this keyword is not used


I am trying build a method(showCost) that takes in the product number and return the cost of the product.My second method is where the error occurs.

I am trying to use the this keyword to point the payment to my local variable payment amount.However, the program says that my paymentamount variable is not used.

 public class CashRegister {
        Double [] cost = {3.0,4.0,5.0,6.0};
        private Double paymentamount;

    public Double showCost (int productnum){
        return (cost[productnum]);
    }

    public void payAmount(double payment){
        this.paymentamount = payment;
    }
}

Solution

  • The program says that paymentamount variable is not used.

    Because: You're just assigning the value in paymentamount variable, but you are not using that variable anywhere else.