The need to provide a cloning operation
Few books teaching Java explain that
it is desirable to provide a cloning operation.
When creating a Person,
we might let a client supply a Date
that is the person's date-of-birth:
Date tBirthDate = new Date(2000, 1, 24);
Person tSomePerson = new Person("Joe", tBirthDate);
where Person is as follows:
public class Person
private String iName;
private Date iDateOfBirth;
public Person(final String pName, final Date pDateOfBirth)
iName = pName;
iDateOfBirth = pDateOfBirth; // share
the Person object
is sharing the Date
supplied by the client.
provides mutable objects,
this may be undesirable.