src/share/classes/java/lang/Object.java

Print this page




 172      * By convention, the returned object should be obtained by calling
 173      * {@code super.clone}.  If a class and all of its superclasses (except
 174      * {@code Object}) obey this convention, it will be the case that
 175      * {@code x.clone().getClass() == x.getClass()}.
 176      * <p>
 177      * By convention, the object returned by this method should be independent
 178      * of this object (which is being cloned).  To achieve this independence,
 179      * it may be necessary to modify one or more fields of the object returned
 180      * by {@code super.clone} before returning it.  Typically, this means
 181      * copying any mutable objects that comprise the internal "deep structure"
 182      * of the object being cloned and replacing the references to these
 183      * objects with references to the copies.  If a class contains only
 184      * primitive fields or references to immutable objects, then it is usually
 185      * the case that no fields in the object returned by {@code super.clone}
 186      * need to be modified.
 187      * <p>
 188      * The method {@code clone} for class {@code Object} performs a
 189      * specific cloning operation. First, if the class of this object does
 190      * not implement the interface {@code Cloneable}, then a
 191      * {@code CloneNotSupportedException} is thrown. Note that all arrays
 192      * are considered to implement the interface {@code Cloneable}.


 193      * Otherwise, this method creates a new instance of the class of this
 194      * object and initializes all its fields with exactly the contents of
 195      * the corresponding fields of this object, as if by assignment; the
 196      * contents of the fields are not themselves cloned. Thus, this method
 197      * performs a "shallow copy" of this object, not a "deep copy" operation.
 198      * <p>
 199      * The class {@code Object} does not itself implement the interface
 200      * {@code Cloneable}, so calling the {@code clone} method on an object
 201      * whose class is {@code Object} will result in throwing an
 202      * exception at run time.
 203      *
 204      * @return     a clone of this instance.
 205      * @exception  CloneNotSupportedException  if the object's class does not
 206      *               support the {@code Cloneable} interface. Subclasses
 207      *               that override the {@code clone} method can also
 208      *               throw this exception to indicate that an instance cannot
 209      *               be cloned.
 210      * @see java.lang.Cloneable
 211      */
 212     protected native Object clone() throws CloneNotSupportedException;




 172      * By convention, the returned object should be obtained by calling
 173      * {@code super.clone}.  If a class and all of its superclasses (except
 174      * {@code Object}) obey this convention, it will be the case that
 175      * {@code x.clone().getClass() == x.getClass()}.
 176      * <p>
 177      * By convention, the object returned by this method should be independent
 178      * of this object (which is being cloned).  To achieve this independence,
 179      * it may be necessary to modify one or more fields of the object returned
 180      * by {@code super.clone} before returning it.  Typically, this means
 181      * copying any mutable objects that comprise the internal "deep structure"
 182      * of the object being cloned and replacing the references to these
 183      * objects with references to the copies.  If a class contains only
 184      * primitive fields or references to immutable objects, then it is usually
 185      * the case that no fields in the object returned by {@code super.clone}
 186      * need to be modified.
 187      * <p>
 188      * The method {@code clone} for class {@code Object} performs a
 189      * specific cloning operation. First, if the class of this object does
 190      * not implement the interface {@code Cloneable}, then a
 191      * {@code CloneNotSupportedException} is thrown. Note that all arrays
 192      * are considered to implement the interface {@code Cloneable} and that
 193      * the return type of the {@code clone} method of an array type
 194      * {@code T[]} is {@code T[]}.
 195      * Otherwise, this method creates a new instance of the class of this
 196      * object and initializes all its fields with exactly the contents of
 197      * the corresponding fields of this object, as if by assignment; the
 198      * contents of the fields are not themselves cloned. Thus, this method
 199      * performs a "shallow copy" of this object, not a "deep copy" operation.
 200      * <p>
 201      * The class {@code Object} does not itself implement the interface
 202      * {@code Cloneable}, so calling the {@code clone} method on an object
 203      * whose class is {@code Object} will result in throwing an
 204      * exception at run time.
 205      *
 206      * @return     a clone of this instance.
 207      * @exception  CloneNotSupportedException  if the object's class does not
 208      *               support the {@code Cloneable} interface. Subclasses
 209      *               that override the {@code clone} method can also
 210      *               throw this exception to indicate that an instance cannot
 211      *               be cloned.
 212      * @see java.lang.Cloneable
 213      */
 214     protected native Object clone() throws CloneNotSupportedException;