2

What I am trying to understand is class referencing and how that reference changes over the life cycle of a program running. For example

ClassRef ref = new ClassRef();
ref.setVariableOne(1);
ref.setVariableTwo(2);

So I know that this will create an object named ref which I can pass around in my code to where I need it, I can use serialization to save and load it which is fine. If I were to create some kind of output it would show that ref = 1,2

But what about if I were to do this

ArrayList<ClassRef> refArray = new ArrayList<ClassRef>
refArray.add(ref);
ref.setVariableOne(3);

I have added my object to the array and then changed one of the variables, does the object in the array also update, or does it "save" that as a snap shot at the time it was added, so if I were to output from the array it would give ref = 1,2 or would it show that ref = 3,2

Fluke
  • 63
  • 5

3 Answers3

1

You're are having this confusion, because you are considering ref as an object, while ref is a reference to an Object. More of it, here

Thus, when you pass ref to a method, you are not passing the object, but the reference to that Object. Thus, the ArrayList contains a reference to the Object. Now, even if you make changes to the Object, the reference remains the same.

So, when you do ref.setVariableOne(3), you don't change the reference. As the reference is what we passed, the changes affect even the ArrayList.

Thus, the output would be ref=3,2.

Community
  • 1
  • 1
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
1

I have added my object to the array and then changed one of the variables, does the object in the array also update, or does it "save" that as a snap shot at the time it was added, so if I were to output from the array it would give ref = 1,2 or would it show that ref = 3,2

The object in the array list will also change and thus, it outputs ref=3,2.

To understand this behaviour you need to understand what are objects and references. An object is things that you can create using new. Here, you created a new object:

new ClassRef();

Note that the process of creating an object does not include the ClassRef ref = part because that part creates a reference to the object. That part says "ref will now refer to the newly created ClassRef object".

When you add ref to the array list, you are adding the reference to the array list. So now the sole element in the array list refers to the object you created in the first, the same object ref refers to. Note that no new objects are created.

When you mutate ref:

ref.setVariableOne(3);

It mutates the object that ref refers to, which just so happens to be the same object that the sole element in the array list is referring to. If you now do refArray.get(0), it is equivalent to just doing ref.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Thank you for this, I am still wrapping my head around classes and objects so this makes alot of sense. – Fluke Mar 19 '17 at 14:07
  • @Fluke I think my answer answers your question please consider accepting it by clicking on that checkmark! – Sweeper Mar 19 '17 at 14:09
0

The output will be ref=3,2

What you need to understand is that when you are adding the ref to the refArray i.e., you are just keeping the reference to the object (pointed by ref) inside the refArray list.

So any changes to the original object will also be noticed when you retrieve the object from the refArray.

Vasu
  • 21,832
  • 11
  • 51
  • 67