Rohit W. Tawde wrote:So you add the copy of the reference s to the list, say 'p'.
Correct. At least if I understand what you're trying to say.
s is a reference variable. Its value is copied and passed to the add method, which in turn copies it to place it into its list structure, so let's call the list's copy 'p'. So now the list has a reference (p) with the same value as the s variable. Both s and the p point to the same object.
So if we change the object to which the reference s is pointing, it has no effect on the reference added to the list ie 'p'.
Correct. If we do
s = something_else; that just puts a different value into s. That is, s now points to a different object, or has the value null, so it doesn't point to any object. That has no effect whatsoever on the list's 'p' reference, or on the object that s previously pointed to.
And the only way we could change the object in the list is by name.add(2,"Ronan") OR name.set(2,"Ronan").
Depends what you mean by "change the object." The phrase is a bit vague. However, if you mean "The only say to change which object that list element points to..." then, yes, adding at that position (or before that position, so it has to shift), or setting at that position, or calling remove() on that position or a preceding one, or clearing the list.
In short, the only way to affect which object a given element of the list refers to is through the list's methods.
Note, however, that if we our original value in our s variable, and then we do something like
s.someField = something or
s.setSomeField(something), since it's the same object that the list is pointing to, the list
will see that change.