Versions Compared

Key

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

...

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 guideline rule "OBJ08-J. Provide mutable classes with copy functionality to 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 guideline rule "OBJ08-J. Provide mutable classes with copy functionality to allow passing instances to untrusted code safely."

...

As a result, the original array values cannot be modified by a client. Note that a manual deep copy could be required when dealing with arrays of objects. This generally happens when the objects do not export a clone() method. Refer to guideline rule "OBJ14-J. Defensively copy mutable inputs and mutable internal components" for more information.

...

Neither the original array values nor the public list can be modified by a client. For more details about unmodifiable wrappers, refer to guideline rule "void SEC14-J. Provide sensitive mutable classes with unmodifiable wrappers." This solution still applies if the array contains mutable items instead of String.

...