• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Creating a copy of an object  RSS feed

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm placing an object in a collection (map). How can I create a copy of the object to place on the map so that I will 2 independent objects?

Thanks in advance.
 
Rancher
Posts: 42973
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can clone() the object. If you do that, make sure you understand the difference between deep cloning and shallow cloning.
 
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 ways:

1] clone() method (shallow copy).

2] write object into byte stream and read agian into new reference (deep copy and computationally inefficient)

3] copy constructor (make a constructor that take object of same class, prepare new object in that, good thing is that, you have control over fields you want new (how deep copy you want).

hope it helps.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
3 ways:

1] clone() method (shallow copy).

2] write object into byte stream and read agian into new reference (deep copy and computationally inefficient)

3] copy constructor (make a constructor that take object of same class, prepare new object in that, good thing is that, you have control over fields you want new (how deep copy you want).

hope it helps.


The clone() method isn't necessarily limited to making a shallow copy. You can override the clone() to make a deep copy the same way as the copy constructor described in 3.

Layne
[ July 29, 2005: Message edited by: Layne Lund ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would put my two cents in and say that overriding clone() is the smartest thing to do. Override it, proceeding in the same manner as if you were writing a copy constructor.

In Java, just get used to calling clone() instead of the copy constructor. This has the virture of keeping your interface one method smaller, since Java is going to supply a clone() method whether you like it or not.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See "Appendix A" to Bruce Eckel's Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ319.htm
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by John M Morrison:
I would put my two cents in and say that overriding clone() is the smartest thing to do. Override it, proceeding in the same manner as if you were writing a copy constructor.

In Java, just get used to calling clone() instead of the copy constructor. This has the virture of keeping your interface one method smaller, since Java is going to supply a clone() method whether you like it or not.



It is important to know that in order to use the clone() method you probably need to override. Object declares clone() as protected, so this limits who can call clone() on your custom class.

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!