• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone() mehtod

 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is clone() method? What are its uses and why we have to use it?
 
Mahesh x Bogadi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clone() method is used to make a duplicate Object of the Object.
Ex X a = new X();
X b = a.clone();

means b is a duplicate of a;
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why the following error?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh's example omitted a crucial point: clone() returns an Object, and you have to cast the returned object to the correct type:

Cloneex dup = (Clonex) ex.clone();

Now, there are three other problems with the example: you'll get a CloneNotSupportedException when you run it. To make something cloneable, you have to declare that the class "implements Cloneable". If you don't, clone() will refuse to work. You generally also need to override clone() to make it public, because the one in Object is protected.

Finally, your variable "x" is static. A single copy of a static variable is shared by all instances of a class, so this isn't a very interesting test of cloning as it stands. To show that two objects are the same, you'd want to make that variable and method non-static, so they belong to the individual instances.

So the classic example of clone() looks like

 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niyas Ahmed Sheikh:
What is clone() method? What are its uses and why we have to use it?


Hi Niyas,
Well, basically the clone method is used to clone an object.
This method is provided by the Cloneable interface of the Object class of the java.lang package.

It is used to create a clone of an existing object.

By clone, we mean, the exact state-replica of an existing object which means that the new object is constructed with exactly the same state as an existing one.
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Ernest Friedman-Hill, Sherry Jacob and Mahesh
 
Niyas Ahmed Sheikh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting error message



Error emssage
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get your code to work you need to place the throws clause on the main method.

Regards,
JD
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also change the line in your clone() method to read:

return super.clone();

Regards,
JD
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get your code to work you need to place the throws clause on the main method.
Or enclose it in a try/catch block...

</nitpick>
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niyas Ahmed Sheikh:
I am getting error message



Error emssage


Ya Steves right...and I suppose it's the best method too...enclose the code in the try-catch block.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic