How do I make defensive copies of a Mutable Object which contains a mutable field in an Immutable Object?
class ImmutableObject {
private final MutableObject immutable_field;
ImmutableObject(MutableObject y) {
this.immutable_field = y;
}
}
class MutableObject {
public int mutable_field;
}
- The MutableObject does not have a constructor that lets me set the field.
- The MutableObject's current state should be captured in the Immutable Object and never changed.
What you need to do is in
Change to:
For an explanation see http://www.javapractices.com/topic/TopicAction.do?Id=15