• Post Reply Bookmark Topic Watch Topic
  • New Topic

DOUBT REGARDING OBJECT REFERENCES  RSS feed

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//--------------------------
class1 object1 = new class1();
class1 object2;

object2=object1;
//------------------------------


now in the code given above, object1 and object2 are acting as references to the same object. so whatever value that you change in object1 will be reflected in object2 also.

now how to prevent this? or how to create object2 as an exact copy of object1 but an autonomous object?

one way is to copy all the member data individually from object1 to object2. Is there any other way?

ARUL JOSE.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please don't shout at us...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!