This week's book giveaway is in the Open Source forum.
We're giving away four copies of RabbitMQ in Depth and have Gavin Roy on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Cloneable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cloneable interface" Watch "Cloneable interface" New topic

Cloneable interface

Nikhil Sun
Ranch Hand

Joined: Nov 13, 2005
Posts: 108
Suppose I want an object to be cloned.So I would implement a public/protected Object clone() method inside its class.My question is whether I need to compulsorly specify that this class implements Cloneable or is it optional?
Sunil Vasudevan
Ranch Hand

Joined: Mar 05, 2007
Posts: 107

So I would implement a public/protected Object clone() method inside its class.

You need not implement Object clone() method inside your class. You just need to make a call.

To answer your second question, yes it is mandatory for the class to implement Cloneable interface.

And best way to figure out whether it is mandatory or not is just to write a test program and run it.

Sunil.V<br />SCJP2, SCWCD1.4, SCBCD1.3
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 42546
You probably would wish to implement a clone() method. There is recent discussion on this thread about how one might do it.
I agree. Here's the link:
subject: Cloneable interface