File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes When and why to use Cloneable interface in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "When and why to use Cloneable interface in java?" Watch "When and why to use Cloneable interface in java?" New topic
Author

When and why to use Cloneable interface in java?

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
What is the use of cloning in Java? When we have to make clone of the object?
Sudheer Maddukuri
Greenhorn

Joined: Nov 12, 2009
Posts: 6

Objects in Java are referred using reference types, and there is no direct way to copy the contents of an object into a new object.

The assignment of one reference to another merely creates another reference to the same object. Therefore, a special clone() method exists for all reference types in order to provide a standard mechanism for an object to make a copy of itself. Here are the details you need to know about cloning Java objects.

Why create a local copy?

The most probable reason for creating a local copy of an object is because you plan to modify the object, and you don't want to modify the method caller's object. If you decide that you need a local copy, you can perform the operation by using the clone() method of the Object class. The clone() method is defined as protected, but you must redefine it as public in all subclasses that you might want to clone.

SCJP5, Preparing for OCPJWCD



Sudheer Maddukuri
Greenhorn

Joined: Nov 12, 2009
Posts: 6

The cloning mechanism is also an implementation of prototype design pattern which is defined as :

The prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to:

avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When and why to use Cloneable interface in java?
 
Similar Threads
Interface
ANY SUGGESTION..
Taglibs and Java Beans
When to use flush() in Java?
When to use seek?