Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clone V/s Creating a new Instance

 
Md. Mohd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic