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?
BigDecimal d = new BigDecimal(this.interval.longValue()); return new MyValueObject(d.add.(augend));no? The question is are they doing something different?BigDecimalobject 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, theaddmethod ofBigDecimalcreates a newBigDecimaland does not change the original one. Is that what your question is about?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.