+ Start a Discussion
Dan Lambeth 31Dan Lambeth 31 

does the standard add() method on a list structure make a new copy of the added item or just store a reference to the item?

this specifically refers to a List of objects, such as account.   
Best Answer chosen by Dan Lambeth 31
Andrew GAndrew G
it would be a reference.
Consider this code example - (run it in dev console as anonymous)
Account[] accts = new List<Account>();

Account a = new Account(Name ='Acme');
accts.add(a);

for(Account x : accts ) {
    System.debug('***DEBUG - City ' + x.BillingCity);
}

a.BillingCity = 'San Francisco';

for(Account x : accts ) {
    System.debug('***DEBUG - City ' + x.BillingCity);
}

insert accts;

a.BillingCity = 'Boston';
for(Account y : accts ) {
    System.debug('***DEBUG - City ' + y.BillingCity);
}


if the account that is added is a copy, the lines where we reset the billing city for the initial record that was instantiated, it would stay null

regards
Andrew