• 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

Why Clonable

 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would I ever need the class Clonable? What is a pratical reason to use it?
-Dale

------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clone objects !!
Sometimes you need your classes to have their own copy of an object so that changing them in other classes do not affect it.
Immutable object like Wrapper classes in java.lang and String do not need to be cloned since their content cannot be changed. But if you pass a Vector containing references to an object (now two objects are referencing that Vector) and you don't want the modifications of one object to be seen by the other one, then you need to clone the Vector.
Let us know if it's still not clear
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since there is no Cloneable class, I assume that you are talking about implementing the Cloneable interface.
If you try to invoke the clone() method on an object whose class does not implement the Cloneable interface you will get a CloneNotSupportedException.
Of course the intent is that the person who writes the class that implements the interface should either write a clone() method that actually WORKS correctly or makes darn sure that the one that gets inherited from Object will do the job adequately for that particular class.
reply
    Bookmark Topic Watch Topic
  • New Topic