Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a copy of an object

 
Raymond Ong
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • 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.
 
ankur rathi
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.
 
Layne Lund
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 ]
 
John M Morrison
Greenhorn
Posts: 21
  • 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.
 
marc weber
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic