Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Mutable classes must provide either a copy constructor or a public static factory method that returns a copy of an instance. Alternatively, final classes may advertise their copy functionality by overriding the clone() method of java.lang.Object. Use of the clone() method is secure only for final classes; nonfinal classes must not take this approach.

...

Code Block
bgColor#ccccff
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-inMake defensive copy
  }

  public Date getDate() {
    return (Date) date.clone(); // Copy and return
  }
}

...

Code Block
bgColor#ccccff
class MutableClass {
  private final Date date;

  private MutableClass(Date d) { // Noninstantiable and nonsubclassable
    this.date = new Date(d.getTime());  // Copy-in Make defensive copy
  }

  public Date getDate() {
    return (Date) date.clone(); // Copy and return
  }

  public static MutableClass getInstance(MutableClass mc)  {
    return new MutableClass(mc.getDate());
  }
}

This approach is useful when the instance fields are declared final.

Compliant Solution (clone())

...

Mutable classes that define a clone() method must be declared final. This ensures that untrusted code cannot declare a subclass that overrides the clone() method so that it supplies to create a spurious instance. The clone() method should copy all internal mutable state as necessary — in this compliant example, the Date object.

...

Compliant Solution (clone() with final members)

When a mutable class's instance fields are declared final and lack accessible copy methods, provide a clone() method, as shown in this compliant solution.

...

Wiki Markup
Callers can use the {{clone()}} method to obtain an instance of such a mutable class. The {{clone()}} method must create a new instance of the {{final}} member class and copy the original state to it. The new instance is necessary because there might not be an accessible copy method available in the member class. If the member class evolves in the future, it is critical to include the new state in the manual copy. Finally, the {{clone()}} method must create and return a new instance of the enclosing class ({{MutableClass}}) using the newly created member instance ({{d}}) \[[SCG 2009|AA. Bibliography#SCG 09]\].

Mutable classes that define a clone() method must be declared final.

Compliant Solution (Unmodifiable Date Wrapper)

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8a4b4b0925672940-19f796f7-4a8e4db1-aa2082a3-b4f2a39042e8e612ccbfb6fb"><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="28515daf257778d1-66753f44-43d6471c-a179939e-faa8bf1083dda51a75fb0a73"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 39. Make defensive copies when needed, 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="356c1b6b2a424f87-c433e7d3-4d274ab3-b035a3ae-265743c206fb55810866b68d"><ac:plain-text-body><![CDATA[

[[Security 2006

AA. Bibliography#Security 06]]

]]></ac:plain-text-body></ac:structured-macro>

...