Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query with Cloning of an Object

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ShallowCloning : A new Object is created , but its variables refer to the same location and the default Object clone method only returns a shallow copy of an Object .



But this gives me (pav1.a) gives me only the value 10 (even though i chnaged to 20 with pav2.a=20)

why the value of a is not copied .


Any ideas ??
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


int are primitive types and are not obejcts so they dont live in heap. Clone will just copy the value of int and create a new int. Any changes to primitive types will not be reflected.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Balu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic