• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best way to get copy of an object?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the best way to get a copy of an object, in terms of performance, best practices, efficiency and everything...

1] clone() method.
2] create new instance of that object and populate with same value.
3] copying object into stream and then reading from stream.

Please help me out.
If you know any other way also to do this please tell me.
Thanks a lot.
[ May 31, 2005: Message edited by: rathi ji ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you want a "deep" copy (all reference variables point to new objects), I would go with 2.
1. clone will only produce a shallow copy
3. serialization requires way too much extra object creation

For 2, why not a constructor that takes an existing object reference as input - naturally this involves more program code but you can control how deep the copy is.
Bill
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



AddressBook ab1 = new AddressBook();

Shallow Copy:
------------
AddressBook ab2 = ab1.clone();
Does it means that two address book object having single person object.

Deep Copy:
---------
New address book object will have its own person object.

Please correct me if I am wrong.

Thanks a lot.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, "shallow" copy means the reference in the copy points to the same object as the reference in the original. "Deep" copy means the copy has references to completely separate objects.
Whether or not this is important depends on the types of the references - it won't matter for String since String objects are immutable, there is no need to create a new String copy since the character contents can't be changed.
Bill
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot William.
It was really nice explanation.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic