Lakshitha Kanchana
Lakshitha Kanchana

Reputation: 1235

how to use same reference variable to store multiple objects at different times without causing any affect to previously stored objects?

Let's say i have 4 previously instantiated objects called o1, o2, o3, o4 of type T. I also have one reference variable called vForAll of type T. First I store o1 in vForAll as

VForAll = o1;

Then I want to store o2 in vForAll without doing any affect on the o1 object and so on! What can I do?

Upvotes: 0

Views: 278

Answers (2)

Alwyn Schoeman
Alwyn Schoeman

Reputation: 466

When you do vForAll = o2, vForAll will no longer point to o1, but o2. Any changes you now make will only affect o2.

Want to change o1 after that, just do a vForAll = o1.

Upvotes: 0

lukeg
lukeg

Reputation: 4399

If I understand the question correctly, if o1, o2, o3, o4 are just reference types, doing vForAll = o2 should do the trick. The o1 will not be modified.

Upvotes: 2

Related Questions