Click to See Complete Forum and Search --> : Java variables and constructors


JoeyJoeJo
09-20-2004, 04:51 PM
Here is another newbie java question for you guys. I have an assignment where inside of a class I am doing banking functions. They are really simplistic equations (i.e. balance = balance - withdraw, etc). Inside my constructor I have one method that calculates the balance + a $200 deposit and then prints out. Then I have a withdraw method right below that that I want to do something like balance = balance - withdraw. In the deposit method I returned balance, but for some reason I can't get balance to come down to the withdraw method.

Here is what my code kind of looks like

public double deposit(double balance, double deposit)
{
balance = balance + deposit;
System.out.println(balance);
return balance;
}

public double withdraw(double withdraw)
{
balance = balance - withdraw;
System.out.println(balance);
return balance;
}


But when I try to compile it says cannot resolve symbols for the line balance = balance - withdraw;

This isn't my real code because this is an assignment for class. Pretty much I just need to find out how to pass variables from one method to another. Thanks a lot for your help.

Jata
09-20-2004, 05:24 PM
It seems to be because you aren't declaring balance anywhere in the withdraw method. Unless balance is passed to withdraw as a parameter or balance is global it won't be able to resolve it.

bwkaz
09-20-2004, 08:24 PM
You're going to have to manually share the value between the two functions.

This can be done by either storing it in the functions that call both of these functions (and making that function pass by reference to your deposit function, instead of by value), or by storing it in your account class (which I assume these functions are members of).

JoeyJoeJo
09-20-2004, 09:00 PM
I thought that I would have to manually share it. I just wasn't sure if there was some super secret java trick I didn't know about. Thanks bwkaz.