File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes The Clone method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Clone method" Watch "The Clone method" New topic
Author

The Clone method

leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Hi All
When would the Clone method of Object come handy? and why?
I mean, in what case would I prefer to implement the Clonable interface, and call the clone method rather than create a new Insance of the Object?

thanks
Vaibhav Mittal
Greenhorn

Joined: Jun 11, 2008
Posts: 23
Creating a clone of an object and creating a new object are two different things. Lets say you have a class Car that has member variables like colour, model, and the distance it has travelled. On creating a new object the values these member variables take are the ones you initialise them with in the constructor. But if you create a clone of the object then the clone has the same values for the member variables as the original object had. Hope that answers your question. If you have any doubts please let me know.


"Life is good"
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
Thanks, Vaibhav Mittal, it makes a lot of sense!
But another question if I may, when would I use a clone? why not to use the original Object?
Vaibhav Mittal
Greenhorn

Joined: Jun 11, 2008
Posts: 23
You would want to use a clone when you want to have an object that is in the same state(by state i mean the values for the member variables is the same).
Lets say you have a situation where you might want to pass an object reference as an argument to a method but you do not want the state of your object that you pass as argument to change. In this case you can create a clone of that object and send it as an argument while you can retain your object as it is. Frankly, i have no idea why one would want to do this. But there could be a scenario where it is necessary. The important thing is know is that there is a difference between sending a cloned object reference as an argument and sending the original object as an argument. Hope that helps.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

a LOT of stuff in Java (or any other language) falls under the category "That seems like a waste of time until I need it, and then I COULDN'T LIVE WITHOUT IT".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
Do a search for Joshua Bloch Effective Java; there used to be a sample chapter available free of charge on the net. You need to find the older (1st) edition. If you find that free sample chapter, it says quite a lot about Object#clone().
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
I was about to write a longish reply to this (I actually did...), but then found articles that describe the case for Cloneable/clone() vs copy constructor better than I can. See
http://www.artima.com/intv/bloch13.html
http://mkseo.pe.kr/blog/?p=1704
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The Clone method
 
Similar Threads
Why Clone method is Protected
Array clone
clone() method
How to Clone an Object??
why only clone method and finalize method in object class are protected?