my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Creating a copy of an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a copy of an object" Watch "Creating a copy of an object" New topic
Author

Creating a copy of an object

Raymond Ong
Ranch Hand

Joined: Jul 17, 2005
Posts: 46
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
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
You can clone() the object. If you do that, make sure you understand the difference between deep cloning and shallow cloning.


Ping & DNS - my free Android networking tools app
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
John M Morrison
Greenhorn

Joined: Jul 25, 2005
Posts: 21
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

Joined: Aug 31, 2004
Posts: 11343

See "Appendix A" to Bruce Eckel's Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ319.htm


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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
 
 
subject: Creating a copy of an object
 
Similar Threads
Use Generic on Map
repainting JLabels on a JPanel
Logical error in creating a HashMap
Caused by: java.lang.IllegalStateException: No modifications are allowed to a locked ParameterMap
about clone