2

If I do this [quasi-java-code]:

while (loop)
{
    localObject = getDataForObject();
    globalPublicStaticArrayList<Object>.add(localObject);
}

All the elements in globalPublicStaticArrayList are identical, equal to the last copy of localObject added. I stepped thru the loop in the debugger and saw that as soon as an Object is added, it is copied in to all the elements of the globalPublicStaticArrayList.

The workaround I found is:

while (loop)
{
    localObject = getDataForObject();
    globalPublicStaticArrayList<Object>.add(new Object(localObject.member1, localObject.member2,...));
}

Has it something to do with pass-by-reference in Java? How come the elements are identical in the first case? Thanks.

Mob
  • 10,958
  • 6
  • 41
  • 58
iceman
  • 826
  • 13
  • 25

3 Answers3

5

Java uses call by value, but here those values are references to objects.

What you are adding to the list is not a copy of the object, but a copy of the reference. Your method returned the same object each time you called it. It probably should return a new object each time, then you wouldn't need this workaround.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • thanks for your insight. If you are referring to getDataForObject(), it is just a place holder in the question. I can see in the debugger/log that localObject holds different data for each iteration. The only difference is in the add() call. This is java.util.ArrayList.add(); – iceman Nov 09 '12 at 13:46
  • @iceman: `I can see in the debugger/log that localObject holds different data for each iteration.` That tells you nothing. It can contain different data but still be the same object. Analogy: Your house can contain different furniture each day, but it's still the same house. Changing the furniture does not make it a different house. – Mark Byers Nov 09 '12 at 13:47
  • Ok. I get you. My question now is, after each pass, or rather after each add() call, all elements in globalPublicStaticArrayList become the same? It's like this: Pass 1: localObject = 1 globalPublicStaticArrayList = [1]; Pass 2: localObject = 2 globalPublicStaticArrayList = [2, 2]; Pass 3: localObject = 3 globalPublicStaticArrayList = [3, 3, 3]; Pass 4: localObject = someValue globalPublicStaticArrayList = [ someValue, someValue, someValue, someValue]; Pass N: localObject = finalValue globalPublicStaticArrayList = [ finalValue * N times]; – iceman Nov 09 '12 at 13:51
  • @iceman: You have many references to **the same object** in your list. To create a new object use the `new` operator. Like this: `localObject = new ....;`. – Mark Byers Nov 09 '12 at 13:58
  • Got it. Instead of ArrayList, I get ArrayList. Thanks for your effort :) – iceman Nov 09 '12 at 14:05
2
globalPublicStaticArrayList<Object>.add(localObject);

here you are passing the localObject reference. You you want a copy of every objects you should create a new object at every iteration

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

In getDataForObject() may be you are not creating "new" object of return type. Because of that all objects are pointing to same address.

Rahul Techie
  • 363
  • 2
  • 8