2

The Setup: I am attempting to write a value object, so I figured it would be best to make it immutable. This object has a BigDecimal, so:

public class MyValueObject {
    private final BigDecimal bob;

    public MyValueObject() {
        bob = new BigDecimal(0);
    }
}

I have also written a handful of methods, including an add method, that return new MyValueObjects. Here is an example of one:

public MyValueObject add(BigDecimal augend) {
        return new MyValueObject(this.bob.add(augend);
}

The question is, does this effectively set bob or is it returning a completely new MyValueObject with an entirely new BigDecimal as expected?

4
  • Well if it didn't function this way then how would you create immutable objects? Commented Jan 12, 2015 at 18:26
  • I could also have written the method as something like: BigDecimal d = new BigDecimal(this.interval.longValue()); return new MyValueObject(d.add.(augend)); no? The question is are they doing something different? Commented Jan 12, 2015 at 18:28
  • 2
    The BigDecimal object is itself immutable (the javadoc says so). So if you're worried that your operation will change the instance variable in your value object, you don't need to. Specifically, the add method of BigDecimal creates a new BigDecimal and does not change the original one. Is that what your question is about? Commented Jan 12, 2015 at 18:30
  • Ahhh, so it wouldn't matter how I got an instance of BigDecimal, it will always be a new one. I was vaguely under the impression that it was mutable for some reason, but never thought to check the javadoc. You know, this thing I've got open here next to my IDE windows. Go figure. Commented Jan 12, 2015 at 18:33

2 Answers 2

2

If you use "new", you are creating a new object. So It is returning a completely new MyValueObject which utilizes "bob", but is not the same.

Sign up to request clarification or add additional context in comments.

3 Comments

So it is utilizing it's own instance of bob, correct?
yes, it would be using its own instance of "bob." I'm assuming the method is in class MyValueObject by the way, otherwise the code wouldn't make sense.
Yes, the method is, your assumption is correct. Thanks for helping clarify that in my head. I'll accept your answer as soon as SO allows.
1

I didn't have a Java editor around so forgive the example. Sometimes it is kind of useful to use a static "builder" in cases like this and make the constructor private.

public class ValueObject {
    private int bob;

    private ValueObject(int bob) {
        this.bob = bob;
    }

    public static ValueObject Create(int value){
        return new ValueObject(value);
    }

    public ValueObject Add(int increaseBy) {
        return ValueObject.Create(this.bob + increaseBy);
    }
}

I realised I didn't answer the question. You would be creating a new object. My answer was intended to make the "creating" more clear in the code which will make it more clear to you.

2 Comments

This appears to be a good answer to an entirely different question.
UPDATE AFTER EDIT: Got you. That did help. Thanks!

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.