• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Not able to deep copy an object.  RSS feed

 
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I read this article about how to do a deep copy but I am unable to do it. Can anyone please tell me what I am missing? This is my code.The output is
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does line 66 do ? Do you really want to do that ?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Burkett wrote:What does line 66 do ? Do you really want to do that ?


Oops!!! I missed that. Its a copy paste problem. Thanks for pointing.

Thank you Mr. Adrian.
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:Hi all, I read this article about how to do a deep copy but I am unable to do it.


It might be worth pointing out that the article in question is 13 years old. I don't think anyone would recommend this method now (and the author himself acknowledges that it's fearfully slow).

However, if this is just a mental exercise, best of luck.

Winston
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:This is my code...


chaitanya,

Please don't put very long lines inside code blocks (I suggest you re-read the UseCodeTags page). I've broken up the offending ones now, but please remember for the future.

Thanks

Winston
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure Mr. Winston. Since I am not sure where the problem is I posted the entire question. However I have a habit of posting the entire code instead of posting it later when someone asks.

Thanks I will not repeat.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

chaitanya karthikk wrote:Hi all, I read this article about how to do a deep copy but I am unable to do it.


It might be worth pointing out that the article in question is 13 years old. I don't think anyone would recommend this method now (and the author himself acknowledges that it's fearfully slow).

However, if this is just a mental exercise, best of luck.

Winston

I started it as a mental exercise. How are developers doing it now a days then?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I have to do it myself manually? Is this the correct approach? I have referred few sites.
 
Winston Gutkowski
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

chaitanya karthikk wrote:Thanks I will not repeat.


OK, no probs.

As to your other questions, the answer is that a true deep copy usually involves rolling your own; although in general, its OK to copy references to immutable classes such as String, since a change to one object will not affect a copy (which is usually why you do it). The real problem comes with arrays of mutable objects, for which there is no simple solution.

At first glance, your latest clone() method would appear to satisfy all the requirements.

Winston
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaitanya, I'd add the following lines to the code in your last post:

The reason is that if you ever subclass the Student class and forget to override the clone() method, the subclass instance would get silently converted into the Student instance by the clone() method. As there is no mechanism to force subclass to override a method, it is better to have at least the runtime check.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:You could also use this:

http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#cloneBean(java.lang.Object)

WP

Hi William, thanks for your reply. I read somewhere in coderanch itself that we should not use BeanUtils to perform a deep copy because it causes problems. I don't remember the actual reason. But I am sure that BeanUtils are used in large frameworks like struts.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Martin Vajsar wrote:Chaitanya, I'd add the following lines to the code in your last post:

The reason is that if you ever subclass the Student class and forget to override the clone() method, the subclass instance would get silently converted into the Student instance by the clone() method. As there is no mechanism to force subclass to override a method, it is better to have at least the runtime check.

Hi Mr. Martin, I dint understand why you added the if condition even though you explained the reason. I could not understand it. Can you please explain?

How will the subclass silently converted to Student class? Can you please explain with a an example?
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chaitanya

Further to Martin's post, imagine you had a subclass of Student, called CollegeStudent.



What type would a developer expect the clone method to return here?
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Mr. James. I understood now. The user actually expects a CollegeStudent, but it returns Student type.
 
Sheriff
Posts: 21742
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why clone() should start with calling super.clone():
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks to Adrian, Winston, Martin, Rob and everyone. I love coderanch.
 
Rob Spoor
Sheriff
Posts: 21742
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of good points on how to use the Clonable interface and properly override Object.clone(), but if given the choice I'd sidestep the clone mechanism altogether for an entirely new type (hierarchy).
There are just too many pitfalls involved, and the fact that responsabilities inferred by adopting Clonable into a type hierarchy are poorly documented makes things even worse. I'd sooner opt for the "rolling your own" approach that Winston mentioned earlier, such as a copy-constructor or a copy factory method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!