...
The clone()
method returns a copy of the original object that reflects the state of the original object at the moment of cloning. This new object can be used without exposing the original object. Because the caller holds the only reference to the newly cloned instance, the instance fields cannot be changed without the caller's cooperation. This use of the clone()
method allows the class to remain securely mutable. (See rule "OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code safely.")
The Point
class is declared final
to prevent subclasses from overriding the clone()
method. This enables the class to be suitably used without any inadvertent modifications of the original object. This solution also complies with rule "OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code safely."
Noncompliant Code Example (Arrays)
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="467ce88415d7cb25-19f2038a-41a04624-948a9210-919ea055cd41837f2f7d9438"><ac:plain-text-body><![CDATA[ | [java:[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 13: Minimize the accessibility of classes and members | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="a9a849b6ac970e25-95ac2531-4c874ac4-9a8da56b-3f0d7df903e1dad8f0a364f4"><ac:plain-text-body><![CDATA[ | [java:[Core Java 2004 | AA. Bibliography#Core Java 04]] | Chapter 6 | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f2c044e3d0ba3bad-98e6ee55-497e4132-9abcb81c-2e4df8552d2a52626272e1d9"><ac:plain-text-body><![CDATA[ | [java:[JLS 2005 | AA. Bibliography#JLS 05]] | [§4.12.4 "final Variables" | http://java.sun.com/docs/books /jls/third_edition/html/typesValues.html#4.12.4] ]]></ac:plain-text-body></ac:structured-macro> |
| ||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="abe7aead836fa470-a70ba049-429643d1-bdd6a583-989ebfcaf0a64bb8c74c4d89"><ac:plain-text-body><![CDATA[ | [java:[Mettler 2010B | AA. Bibliography#Mettler 2010B]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
...
OBJ02-J. Minimize the accessibility of classes and their members 04. Object Orientation (OBJ) OBJ04-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely