aspose file tools*
The moose likes Beginning Java and the fly likes clone() mehtod Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clone() mehtod" Watch "clone() mehtod" New topic
Author

clone() mehtod

Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
What is clone() method? What are its uses and why we have to use it?
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
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

Joined: Jun 15, 2005
Posts: 129

Why the following error?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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



[Jess in Action][AskingGoodQuestions]
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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.


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
Thank you very much Ernest Friedman-Hill, Sherry Jacob and Mahesh
Niyas Ahmed Sheikh
Ranch Hand

Joined: Jun 15, 2005
Posts: 129
I am getting error message



Error emssage
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
To get your code to work you need to place the throws clause on the main method.

Regards,
JD
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
You should also change the line in your clone() method to read:

return super.clone();

Regards,
JD
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Jun 29, 2005
Posts: 128
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: clone() mehtod