...
Code Block | ||
---|---|---|
| ||
public final class MutableClass { // Copy Constructor
private final Date date;
public MutableClass(MutableClass mc) {
this.date = new Date(mc.date.getTime());
}
public MutableClass(Date d) {
this.date = new Date(d.getTime()); // Copy-in
}
public Date getDate() {
return (Date) date.clone(); // Copy and return
}
}
|
...
Code Block | ||
---|---|---|
| ||
class MutableClass {
private final Date date;
private MutableClass(Date d) { // Noninstantiable and nonsubclassable
this.date = new Date(d.getTime()); // Copy-in
}
public Date getDate() {
return (Date) date.clone(); // Copy and return
}
public static MutableClass getInstance(MutableClass mc) {
return new MutableClass(mc.getDate());
}
}
|
...
Code Block | ||
---|---|---|
| ||
public final class MutableClass implements Cloneable { private Date date; public MutableClass(Date d) { this.date = new Date(d.getTime()); } public Date getDate() { return (Date) date.clone(); } public void setDate(Date d) { this.date = (Date) d.clone(); } public Object clone() throws CloneNotSupportedException { final MutableClass cloned = (MutableClass) super.clone(); cloned.date = (Date) date.clone(); // manually copy mutable Date object return cloned; } } |
...
Code Block | ||
---|---|---|
| ||
public final class MutableClass implements Cloneable { private final Date date; // final field public MutableClass(Date d) { this.date = new Date(d.getTime()); // copy-in } public Date getDate() { return (Date) date.clone(); // copy and return } public Object clone() throws CloneNotSupportedException { Date d = (Date) date.clone(); MutableClass cloned = new MutableClass(d); return cloned; } } |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3bef9e089766f298-eca292a2-4dfe43e1-86d2a1ae-aba0a83d0d49d930fa292193"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [method clone() | http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()] | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="070a90b4ed74ea21-d2a9d0d4-4dd743ce-ad668161-c1dec184b24ce6bc94e8f976"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 39: Make defensive copies when needed and Item 11: Override clone judiciously | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="342c0cf3d2f78357-0320f0bd-43ca44d0-8002b196-1f240ab24cec1d00eac6c4c3"><ac:plain-text-body><![CDATA[ | [[Security 2006 | AA. Bibliography#Security 06]] | ]]></ac:plain-text-body></ac:structured-macro> |
...