How do I make defensive copy of an object?

Posted by kunjaan on Stack Overflow See other posts from Stack Overflow or by kunjaan
Published on 2010-06-02T03:24:08Z Indexed on 2010/06/02 3:33 UTC
Read the original article Hit count: 146

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;
  }

  MutableObject return_immutable_field() {
    return immutable_field;
  }
}

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about defensive-copy