This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

The Clone method

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lowercase baba
Posts: 12792
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Marshal
Posts: 67412
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!