Java method returns the given variable instead of the result

The observed behavior is result of a side-effect. That is, you expected moneyaddition to return the result of money+added and wonder why the function returned just the added value instead of the sum. There’s nothing wrong with the function your looking at, but in the prior call to walletbalance you’ve never set the money member, so 0 + added makes added. You need to fix the setter for this property, then it works:

public static double walletbalance(double bal) {
    money = bal;
    return bal;
}

Leave a Comment