ArrayList and modifying objects included in it

Primož Kralj picture Primož Kralj · Mar 19, 2012 · Viewed 53.1k times · Source

Say we have an ArrayList myArray. I want to modify an object by calling its function. If I do it this way, will the original object be changed or not?

myArray.get(0).myModyfyingFunction();

To clarify further - I am concerned if get() actually returns a reference to my original object or does it only return a copy of my original object.

Answer

Óscar López picture Óscar López · Mar 19, 2012

get() will return a reference to the object, never a copy. Any modification you do on the returned reference will be made on the object itself