aspose file tools*
The moose likes Java in General and the fly likes Clone V/s Creating a new Instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clone V/s Creating a new Instance" Watch "Clone V/s Creating a new Instance" New topic
Author

Clone V/s Creating a new Instance

Md. Mohd
Greenhorn

Joined: Apr 11, 2006
Posts: 12
Hi Ranchers,
I have the following queries related to clone() method .

1. What is the difference b/w creating a new instance of a class and creating a clone() of that class (assuming the class implements Cloneable).

2. What is the memory and processing impact of each other .

3. Where we have to use the clone() method .

Thanks a lot.
Amit Biswas
Ranch Hand

Joined: Jun 01, 2006
Posts: 52
1.
Creating a new instance is a simple way of creating the object through its constructor. You may create a new object either by using the default constructor or you may have a constructor with arguments that sets some state in the object.
For example: A objA = new A(10);
You may also use a design-pattern like factory avoid instantiation in the style mentioned above.
Creating a clone literally means you can create an object that is similar to another object but is not the same object. By this I mean, when you say you have cloned an object, it means that there is a different object in the heap that has the same state. But if you alter this new object, it does not alter the original object.

2.
I don't think there is a difference in the memory and processing impact for the aforesaid approaches, though I am not sure.

3.
You need to use the clone method if you're going to modify an object, say in a method, without altering the caller's object.

For example: Lets say you the following code:

private void method1()
{
A objA = new A(10); //A is cloneable
try
{
method2(objA);
}
catch(CloneNotSupportedException cnse){//some code}
System.out.println(objA.getNumber());//prints 10

}

private void method2(A object) throws CloneNotSuppotedException
{
A objB = (A)object.clone();
objB.setNumber(20);
System.out.println(objB.getNumber());//prints 20
}

Hope that clarified your doubt.
Md. Mohd
Greenhorn

Joined: Apr 11, 2006
Posts: 12
Hi Amit ,
You have nicely cleared my queries.When searching for the same I found a very useful link below :

http://www.artima.com/objectsandjava/webuscript/ClonCollInner1.html

Please have a look at it.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Has anybody used Java clone() to implement the GoF Prototype Pattern?

I often use configurable factories that map some key to a classname and do a Class.forName().newInstance() using the name. I wondered if we couldn't just as well seed the map with a key and an object instance and have the factory use clone(). Then I wondered why I'd ever do that. Maybe if the various classes had incompatible constructors or setup?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Stan]: Has anybody used Java clone() to implement the GoF Prototype Pattern?

I have, but only when playing around with implementations. Using clone() can allow you to skip copying a lot of fields individually, which I guess is nice if the number of fields is large. However I've never used it in real work - my aesthetic dislike of the Cloneable API has exceeded any other benefits it might offer here.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Clone V/s Creating a new Instance