0

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");
        }
    }
}
4
  • 4
    Show us some more code. Commented May 10, 2013 at 14:10
  • Show us the code for setBalance, and the code that returns the balance from the first class. This might be a call-by-value problem Commented May 10, 2013 at 14:10
  • Show us some code and what you're actually expecting balance to be. I suppose you're rather setting it to 50 whilst intenting to add 50 which would be 100 in total. Commented May 10, 2013 at 14:12
  • Are you dealing with the same instance when you "go back to the class"? Commented May 10, 2013 at 14:14

3 Answers 3

5

In your case

Replace this line:

ZooBalance.setBalance(166);

with:

ZooBalance.setBalance(ZooBalance.getBalance() + 166);
Sytem.out.println(ZooBalance.getBalance()); // 166

You need to make a setter for that class. Assuming the name of the class is TestClass.

class TestClass {
    private int balance = 50;
};
public int getBalance() {
    return this.balance;
};
public void setBalance(int newBalance) {
    this.balance = newBalance;
};

And then in another class:

TestClass test = new TestClass();
test.setBalance(test.getBalance() + 50);
System.out.println(test.getBalance);// 100
Sign up to request clarification or add additional context in comments.

1 Comment

It's almost there, the second class now adds 50 to the first 50 which gives me 100. Which is correct. But, when i return the balance from the method getBalance which is inside the first class, i get 50 (the first value)
2

I'm pretty sure there's a difference between adding 50 to a value and setting a value to 50.

myObject.setBalance(50); will not add 50, it will change the value to 50. To add 50, you'd have to do something along the lines of myObject.setBalance(myObject.getBalance() + 50);

Comments

1

generally there are two ways to this.

First. Use getter and setter:

Zoo zoo = new Zoo();
zoo.setBalance(zoo.getBalance() + 50);

Second. Add an adder method:

public void addToBalance(int addend) {
    balance += addend;
}

Zoo zoo = new Zoo();
zoo.addToBalance(50);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.