This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 42974
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
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!