It is best practice not to expose the internal references of an Object (Entity). So if an Object has a field of type java.util.Date
then for example the getter for this field should return not the original date but an copy of it.
But for an java.util.Date there are two common ways to create that copy:
- clone:
(Date) originalDate.clone()
- copy via constructor
new Date(originalDate.getTime())
My question is, which way is better, and why?