user366930
user366930

Reputation: 13

regarding object recycling

I have a question. What is wrong with regards to the below code:

ArrayList tempList2 = new ArrayList();
tempList2 = getXYZ(tempList1, tempList2);

//method getXYZ
getXYZ(ArrayList tempList1, ArrayList tempList2) {

  //does some logic and adds objects into tempList2

  return tempList2;
}

The code will get executed but it seems by passing tempList2 to the getXYZ method argument, it is doing object recycling.

My question is, Is recycling the tempList2 arraylist object correct?

Upvotes: 1

Views: 575

Answers (3)

Stephen C
Stephen C

Reputation: 718758

My question is, Is recycling the tempList2 arraylist object correct?

I don't quite know what you mean by "recycling". This doesn't appear to be a case where the application is recycling objects in an attempt to avoid allocating new objects. (That is the normal meaning of "recycling" in Java.)

If getXYZ is called multiple times with the same tempList2 object, then this is simply a way of aggregating stuff into a single list. The fact that getXYZ returns an ArrayList leaves open the possibility that the method implementation may be changed to return a different ArrayList instance. That's not a problem per se, but it might be if the caller doesn't assign the result of the call appropriately.

If getXYZ is only called once for any given tempList2 object, then this looks a bit strange.

In summary, this code looks a bit fishy, and is fragile if someone changes the implementation of getXYZ. However, it is not down-right wrong, and there may be some good reason (or historical reason) for doing things this way that is not apparent in the small chunks of code you included in the question.

EDIT - in response to this comment (inlined to make it readable)

Actually, the reason for the above code is, I wanted to avoid creating two arraylist objects. For ex: the conventional method would be

ArrayList tempList2 = new ArrayList(); 
tempList2 = getXYZ(tempList1);

//method getXYZ 
getXYZ(ArrayList tempList1) { 
    ArrayList tempList = new ArrayList();
    //INSTANTIATED ONLY ONCE 
    //does some logic and adds objects into tempList 
    return tempList; 
}

The real conventional way of doing this would be:

ArrayList tempList2 = getXYZ(tempList1);

or

ArrayList tempList2;
// some intermediate lines
tempList2 = getXYZ(tempList1);

Neither of these require creating the unnecessary ArrayList instance of your approach, and neither require passing 2 ArrayList instances into the getXYZ method.

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500105

There's really no such concept as "object recycling" here. Nothing's being garbage collected, if that's what you're interested in... at least, not if there are no lines like this:

tempList2 = new ArrayList();

within getXYZ().

The reference to the ArrayList is passed into the method and then returned. That's all. It's just a reference - it's not the object itself.

Now you could say it's slightly odd for the method to return the ArrayList reference which was passed into it... usually that's used for fluent interfaces, but it doesn't seem to be the case here.

Upvotes: 3

Matthew Flaschen
Matthew Flaschen

Reputation: 284786

You're passing the value of the tempList2 reference. The object itself isn't copied. So when the passed-in tempList2 is modified, the caller sees the changes. This is also called call-by-sharing

So there's nothing wrong, once you understand what's going on.

Upvotes: 0

Related Questions