6

For the purposes of making a copy of an object and getting access to its data, what is better and why?

1. Create a new object and initialize it with the data you want to clone through the constructor

 HashSet<String> myClone = new HashSet<String>(data);

2. Clone object as is and cast it to the type you believe it to be

 HashSet<String> myClone = (HashSet<String>) data.clone();
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 2
    Possible duplicate of http://stackoverflow.com/questions/2356809/shallow-copy-of-a-map-in-java – Mansoor Siddiqui Aug 30 '11 at 19:16
  • 2
    @Mansoor is right: copy constructor is definitely the way to go. – mre Aug 30 '11 at 19:17
  • @mre: Using a copy constructor directly means that the new object will be an object of the specified type, rather than an object whose type matches the object being copied. In some cases, this may be very bad. Calling Vehicle's copy constructor on a ToyotaPrius will yield a Vehicle with vehicle-related properties that match those of the copied ToyotaPrius, but any Toyota-Prius-specific information in the original vehicle will be omitted in the copy. Such semantics may occasionally be desired, but often what would be desired would be a ToyotaPrius whose properties matched the original. – supercat Aug 30 '11 at 19:30
  • @mre: If class has a properly-implemented virtual clone method which derives from Object.Clone, a derived class may either override that method with one that calls base.Clone and then does any necessary "fix-ups", or if no fix-ups are necessary it may simply leave the inherited method alone. If any ancestor's Clone method calls a constructor rather than Clone, then every class with a public clone method must override it to call a copy constructor. Failure to do so will yield incorrect behavior. – supercat Aug 30 '11 at 19:33

3 Answers3

2

Definitely use a copy constructor - clone() is really quite broken (at least most people agree so.) See here for details.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
1

'It depends'. Not all classes have copy constructors. Some classes define clone() and others inherit it from Object.

If you are thinking about how to implement copy semantics in your own class, many people recommend against clone, but others recommend for it. A third alternative is a static factory method that does the work.

If you are trying to make a copy of some existing class, you are at the mercy of the existing implementation. Maybe it has a clone() that does what you want, and maybe it doesn't. Maybe it has a copy constructor, maybe it doesn't.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

Clone doesn't copy the data, it copy the references(Shallow copy). So, if you want to do a deep "copy" and that it became independent from the first one, you'll have to do a "item by item clonning", commonly refered as deep copy (there are several methods to acomplish this).

Also, you might take a look at the clone() method of the class that implements that HashSet. If that class has overriden that method, then it might perform a deep copy. I recommend you this book: http://www.horstmann.com/corejava.html

santiagobasulto
  • 11,320
  • 11
  • 64
  • 88