Here's a for loop I have in my code, which loops through a list of account IDs. It takes each ID and puts it as the first element of a new list. That list is then populated with additional IDs (Populate_All_Children(list)), after which it is inserted into a list of lists ("accGroups_List").

for(ID startID :accID_List){            
        List<ID> tempList = new List<ID>();

I learned from a recent question that lists are passed by reference, which has me wondering whether I'm actually creating a new, separate tempList variable for each iteration of the loop - or if instead the same list is being constantly overwritten.

The goal is to create a new list for each iteration - I'd name each tempList differently if I knew how. What I dont want is for accGroups_List to be filled with pointers to the same list data, which would come from the very last iteration.

Will my code get me what I want?

1 Answer 1


The declaration will create a fresh new list in memory for you and drop the old one.


  • For lists of primitives the list items are unique to the list and can't be passed around by reference, although the list itself is passed by reference.
  • For lists of non-primitives, including sObjects and Apex class instances, the lists and the items they contain are passed by reference.

Therefore, any other pointers that are still open to list items that used to be contained in the tempList will cause those items to still be in memory. But tempList itself will be a clean slate when you call the new again. Same behavior for tempList.clear();

But reading your code it occurred to me that this temp list might not need to be created. What if, for example, you returned List from Populate_All_Children(), like this:

List<Id> tempList = populateAllChildren(startId);

To extend on this, you could simply get this all done in one line with the following:


This keeps your logic nicely separated, like you already have, yet doesn't require maintaining the list in the main loop level.

  • Sure, I could do that - though I would point out that just defers the 'templist' creation to the "populateAllChildren" method, and raises the same question about what happens when a new templist is created from each iterations call to populateAllChildren()
    – smohyee
    Commented Oct 24, 2013 at 17:58
  • Adam, I'm having trouble understanding this part of your response: "However, any other pointers that are still open to list items that used to be contained in the tempList will cause those items to still be effectively in memory..." Can you clarify? If I understand it right, you're saying that if I pass temp_list to another method, that second method creates a copy of the pointer to the list address, and therefore the data in that temp_list stays in memory despite the original pointer wiping out after each iteration.
    – smohyee
    Commented Oct 24, 2013 at 18:01
  • I'm thinking of non-primitives here. My bad. For primitives the list items are just a copy. The list itself is still passed by reference, but the items it contains are unique to it and not pointers to other objects. If the list was of sObjects or Apex class instances, any list that still held onto pointers of the items would keep them in memory.
    – Adam
    Commented Oct 24, 2013 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .