• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clone() mehtod

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is clone() method? What are its uses and why we have to use it?
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why the following error?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting error message



Error emssage
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also change the line in your clone() method to read:

return super.clone();

Regards,
JD
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic