My problem is how do I add a value to a variable from another class, for example:
private int balance;
then later in the methods in the same class, I add values to the balance with no problem using:
balance += 50;
but when I go to another class, and type the object then the setter for the balance:
myObject.setBalance(50);
but the problem strikes here, when I go to the first class and return the balance from there, i get nothing (or the old value of balance) in other words, the NEW value of balance is not being added. Any ideas why? I've been stuck at this point for the last couple of hours
Here's my code:
public class Zoo
{
private int balance;
public void setBalance(int balance)
{
this.balance = balance;
}
public int getBalance()
{
return this.balance;
}
}
MY SECOND CLASS:
public class RandomEvents
{
private Zoo ZooBalance = new Zoo();
public void callEventSix()
{
System.out.println("A local group has raised money to your Zoo");
System.out.println("Would you like to accept the money? (y) or (n)");
Scanner readerEventThree = new Scanner(System.in);
String readerTwo = readerEventThree.next();
if ( readerTwo.equals("y") )
{
ZooBalance.setBalance(166);
System.out.println("You have accepted the gift");
System.out.println("Your new balance is " + ZooBalance.getBalance());
} else if ( readerTwo.equals("n") )
{
System.out.println("You have refused the gift");
}
}
}
balanceto be. I suppose you're rather setting it to 50 whilst intenting to add 50 which would be 100 in total.