BookRags.com Literature Guides Literature
Guides
Criticism & Essays Criticism &
Essays
Questions & Answers Questions &
Answers
Lesson Plans Lesson
Plans
My Bibliography Periodic Table U.S. Presidents Shakespeare Sonnet Shake-Up
Research Anything:        
History | Encyclopedias | Films | News | Create a Bibliography | More... Login | Register | Help
Not What You Meant?  There are 25 definitions for Clone.  Also try: Clone (function).

Clone (Java method)

Print-Friendly
About 1 pages (270 words)

Bookmark and Share Know this topic well? Help others and get FREE products!

clone() is a method in the Java programming language for object duplication. Because objects in Java are manipulated through reference variables, there is no direct way to copy an object (We would be trying to duplicate the reference variable rather than the object we control through that variable). Therefore, a special clone() method exists in the ultimate superclass Object to provide a standard mechanism for duplicating objects. The class Object's clone() method creates and returns a copy of the object. However, clone() throws a CloneNotSupportedException unless the class you are trying to use it on implements the marker interface Cloneable. The default implementation of Object.clone() performs a shallow copy. Classes must override clone() to provide a custom implementation when a deep copy is desired, or some other custom behavior is desired. The syntax for calling clone in Java is:

Object copy = obj.clone();

or commonly

MyClass copy = (MyClass) obj.clone();

which provides the typecasting needed to assign the generic Object reference returned from clone to a reference to a MyClass object. One disadvantage with the design of the clone() method is that the return type of clone() is Object, and needs to be explicitly cast back into the appropriate type (technically a custom clone() method could return another type of object; but that is generally inadvisable). One advantage of using clone() is that since it is an overridable method, we can call clone() on any object, and it will use the clone() method of its actual class, without the calling code needing to know what that class is (which would be necessary with a copy constructor).

View More Summaries on Clone (Java method)
 
Ask any question on Clone (Java method) and get it answered FAST!
Answer questions in BookRags Q&A and earn points toward
discounted or even FREE Study Guides and other BookRags products!
Learn more about BookRags Q&A
Copyrights
Clone (Java method) from Wíkipedia. ©2006 by Wíkipedia. Licensed under the GNU Free Documentation License. View a list of authors or edit this article.

Article Navigation
Join BookRagslearn moreJoin BookRags




About BookRags | Customer Service | Report an Error | Terms of Use | Privacy Policy