0

I have a List which i get like following :

List<MyObject> list  = complexObject.getMyObjectList();
for(final MyObject obj in list){
    // set something in obj. .............(1)
}

I debugged the above code and looked found out the the modified MyObject's are not reflected in the complexObject unless I do something like following..

complexObject.setList(list);

I thought Java does everything by "Pass by reference". Am i missing something. ?

maerics
  • 151,642
  • 46
  • 269
  • 291
user882196
  • 1,691
  • 9
  • 24
  • 39
  • 3
    Do you know the implementation of `getMyObjectList()`? Maybe it's returning a deep copy. – Greg Hewgill Jan 10 '12 at 18:04
  • 2
    Java "passes references by value". – Tom Hawtin - tackline Jan 10 '12 at 18:04
  • 2
    You're missing that Java doesn't pass by reference. (Some argue it does, but except for those who are plain wrong, they just extend the term "pass by reference".) See countless other questions. Let me guess, "set something in obj" is `obj = ...`? –  Jan 10 '12 at 18:05
  • 1
    We can't tell until we know how getMyObjectList works. It could make a list of clones. – Ingo Jan 10 '12 at 18:05
  • @ Tom & delnan : Java passes primitives by value, but objects are passed by reference. – user882196 Jan 10 '12 at 18:12
  • 3
    @user882196: No, references are passed by value. That doesn't mean the objects referred to are copied (you're right to imply they aren't). But pass by reference is much more powerful, it implies a lot more than having several variables, members, etc. refer to the same value. As I said, this has been discussed countless times here (and probably elsewhere). See [Is Java pass by reference?](http://stackoverflow.com/q/40480/395760) for the most popular instance. –  Jan 10 '12 at 18:16

2 Answers2

2

The implementation of the getMyObjectList() function might do a deep copy of the list before returning it:

protected List<MyObject> objects = new ArrayList<MyObject>();
// ...
public List<MyObject> getMyObjectList() {
    List<MyObject> os = new ArrayList<MyObject>();
    for (Object o: this.objects) {
      os.add(o.clone());
    }
    return os;
}

This is a common idiom to avoid accidentally mutating the list and/or members of the list.

maerics
  • 151,642
  • 46
  • 269
  • 291
  • But even with that idiom, one can still access the objects *inside* the list. Also, to prevent accidents it's good to use `Collections.unmodifiableList()` in that case. – Greg Hewgill Jan 10 '12 at 18:07
  • This is a shallow copy. Mutating any MyObject in the list returned by `getMyObjectList()` should still mutate the object referred to from `objects`. – Cameron S Jan 10 '12 at 18:07
  • This wouldn't explain the OP's problem. getMyObjectList would have to clone the objects in the list as well. – JB Nizet Jan 10 '12 at 18:07
  • @JBNizet: ah yes, I misread the question thinking it was about modifying the *list*, not the *contents* of the list. I'll update accordingly. – maerics Jan 10 '12 at 18:09
0

In java your variables like myObject holds a reference to object.

public MyClass myObject = new MyClass();
foo( myObject );

and

public void foo( MyClass _obj )
{
  //some code here
}

So if you pass myObject variable to a function, myObject's value passed by value. But the copy of myObject variable shows the same object with myObject. _obj and myObject has the same value "reference of object".

halit
  • 177
  • 7