Does a List object get passed by reference?

Possible Duplicate:
Is Java pass-by-reference?

Does a List object get passed by reference? In other words, if I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?


in other word: If I pass an ArrayList (java.util.ArrayList) object to a class, will it be automatically updated when I change it?

Yes

Does the List object passed by reference?

Value of reference would get passed

public updateList(List<String> names){
 //..
}

Explanation

When you call updateList(someOtherList); the value of someOtherList which is a reference will copied to names (another reference in method, bit by bit) so now both of them are referring to same instance in memory and thus it will change


See

  • Is Java "pass-by-reference" or "pass-by-value"?

  • If you add to a list in one method, its original reference in first method will also contain the new item.

    java is pass by value, and for objects this means the reference is passed by value.


    Yes, a List that you pass to a method is passed by reference. Any objects you add to the List inside the method will still be in the List after the method returns.

    链接地址: http://www.djcxy.com/p/3516.html

    上一篇: 方法调用后,对象状态不会改变

    下一篇: List对象是否通过引用传递?