• Post Reply Bookmark Topic Watch Topic
  • New Topic

shallow copy and deep copy  RSS feed

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read this statement regarding

Shallow copy: If a shallow copy is performed on obj-1 then it is copied but its contained objects
are not.

I did not understand this statememt , how can one object ahve other objects within it ?

Can i say an array of arrays is an example where we have objects within an object ?
 
gaurav pal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's basically another way of saying that there is HAS-A relation between two classes

Suppose we have following two classes :
class A{
private int a,b,c;
}


public class B{
private int bla1,bla2,bla3;
private A a;
}


now If we create an object of B, and then try shallow copying, then bla1,bla2,bla3 would be copied , but not the content referenced by a
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


beaten to the punch
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:I did not understand this statememt , how can one object ahve other objects within it ?

Arrays, Lists, Collections, Maps, but also non-collection classes. The values of instance fields are also objects contained by an object:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!