When should I use clone() method of Object class in Java?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Monoranjan_Gorai
Posted On:   Monday, April 23, 2007 08:08 AM

Can anybody please give me an example where we use clone() method and why? Can'nt we achieve the save using new operator.

Thanks

Re: When should I use clone() method of Object class in Java?

Posted By:   Anonymous  
Posted On:   Wednesday, April 25, 2007 12:27 PM

clone method of object class is use for deep copy of the object when u want to create new object from existing object that time u can use clone method.




i used clone method in my xml parsing program in which we have all emlpoyee records in single xml file and from that xml file we were create the seperate xml file for each employye so when i extract the emp element from xml file and write it to the new xml file that time error is thrown that element has already root element b'coz we use same object




so we crete the clone of exiting object and write it in to the file




so when u want to create similar object that time u can use clone method




regards kalpesh



www.javaxmlworld.110mb.com


Re: When should I use clone() method of Object class in Java?

Posted By:   java_learner  
Posted On:   Tuesday, April 24, 2007 04:36 AM




Cloning of an object to avoid creation. If the cost of
creating a new object is large and creation is resource intensive, we clone the object. We use the interface Cloneable and call its method clone() to clone the object.




see the below link for more information.



http://weblogs.java.net/blog/emcmanus/archive/2007/04/cloning_java_ob.html

About | Sitemap | Contact