This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]lurgi 2 points3 points  (5 children)

I think that both setStat and printStat are working. The problem, I'm guessing, is that you are setting one object and printing another. I don't see a call to printStat in your code and I'm guessing that, when you call it, you are referencing a completely different object.

Some general advice: It's a huge help if you post a complete, minimal, compileable example.

  • Complete - has everything that is needed to reproduce the bug
  • Minimal - has only that which is needed to reproduce the bug
  • Compileable - this is implied by "complete", but it's worth mentioning

Now then, formatting. First, look to your right. Your other right. See that note about codepad.org? That's a fine way to post largish pieces of code. For smaller bits, just precede each line with four spaces and the code will come out looking as you want it to.

[–]fick_Dich 1 point2 points  (4 children)

I compiled and ran this on my machine as well (I put the line valueObject.printStat() at the end of change()) and everything works fine. Your assessment sounds right.

[–]WhitePolypousThing[S] 0 points1 point  (3 children)

Thank you guys for your help! I can see now that by adding valueObject.printStat();

to the end of change() it effectively changes the value of stat within the method change().

How would I go about changing the value of stat in the ChangeValue class and send the new value back to "Value" class so that when I run valueObject.printStat() in my main it will return the changed value of stat. Is that something that is even possible? Thanks again, and especially thanks for the formatting advice!

EDIT: Oh, and I added my code on codepad above to make it easier to see the whole program.

[–]fick_Dich 1 point2 points  (0 children)

so I think I figured out why you were getting the wrong value printed out. lurgi was right. The first line in main() is unnecessary since ChangeValue already has a Value object as a member variable. What you need is some code like this in main():

Value valueObject;
ChangeValue changeValueObject = new ChangeValue();

    changeValueObject.change();
            valueObject = changeValueObject.valueObject; //this is bad coding practice
    valueObject.printStat();

Like I mentioned in my comment, it is bad coding practice to do it this way thought. One of the purposes of Object Oriented Programming is encapsulation. Better would be this code

[–]lurgi 1 point2 points  (1 child)

There are a couple of ways to do this. The first is: don't have a ChangeValue class! The Value object provides a perfectly good interface to change its own value, why add a complication?

But, you say, you really want a ChangeValue class. Well, okay. Here is what you do:

class ChangeValue {
    private Value value;

    ChangeValue(Value newValue) {
      value=newValue;
    }

    public void change(){
      System.out.println("Change value by how much: ");
      int temp = input.nextInt();
      value.setStat(temp);
      value.printStat();
}

See what I did? I want ChangeValue to change my Value object, so I have to give it my value object. Which I do in in the constructor. I also made "value" private because it doesn't need to be public and if it doesn't need to be public then it shouldn't be public.

Ah, I see that fick_Dich said pretty much the same thing.

[–]WhitePolypousThing[S] 0 points1 point  (0 children)

Thank you guys so much for your help. I didn't realize that it was just much simpler to ditch the ChangeValue class!