• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deep Copy in java  RSS feed

 
chnmaverick pionner
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
class two implements serialization
{
int b;
}
Class one
{
int a;
two twoobject;
}

class demo
{
one oneobj1=new one();
one oneobj2;
//making deep copy using serialization(oneobj1 is copied to oneobj2)
}

If I deep copy oneobj1 to oneobj2 using serialization whether different copies of oneobj1 and oneobj2 will be created??
If I change the twoobject inside oneobj1 whether it will affect twoobject inside oneobj2??

Which is the best approch ??

 
Devaka Cooray
Marshal
Posts: 5650
765
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chnmaverick pionner wrote:
Which is the best approch ??

Cloning Objects - That might be what you need to do.
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chnmaverick wrote:If I change the twoobject inside oneobj1 whether it will affect twoobject inside oneobj2??

Whether it's deep copy or shallow copy it will not change what one object refers to by changing other's same reference. But with shallow copy if you change the state of objects an object refers to then both (i.e: original and cloned) will see the changes.
 
chnmaverick pionner
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do you mean by change in state of objects?
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chnmaverick pionner wrote:what do you mean by change in state of objects?

That is properties of the objects referred by the original object being cloned.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!