previous | start | next

## Deep Cloning

- Why doesn't `clone` make a deep copy?
  Wouldn't work for cyclic data structures
- Not a problem for immutable fields
- You must clone mutable fields

    public class Employee
      implements Cloneable
    {
       public Object clone()
       {
          try
          {
             Employee cloned = (Employee)super.clone();
             cloned.hireDate = (Date)hiredate.clone();
             return cloned;
          }
          catch(CloneNotSupportedException e)
          {
             return null; // won't happen
          }
       }
       ...
    }

previous | start | next