Sub topic 15
References, clone, and deepClone
This chapter assumes you can write basic Apex, know that Account and Contact are SObjects, and have used the List, Set, and Map types from earlier chapters. It covers what happens when you assign a collection to a second variable, and how to make a real copy.
Collections are reference types
A variable of type List, Set, or Map holds a reference to the collection in memory. It does not hold the elements themselves. Assigning one variable to another copies the reference, so both variables point to the same collection.
List<String> original = new List<String>{'Ada', 'Grace'};
List<String> alias = original;
alias.add('Linus');
System.debug(original.size()); // 3
System.debug(original == alias); // trueThe same applies when you pass a collection to a method. The method can add or remove elements, and the caller sees the change. Reassigning the parameter inside the method does not affect the caller's variable.
Making a shallow copy with clone
Use clone() to get a new collection with the same elements. Changes to the new collection's structure, such as adding or removing elements, do not affect the original.
List<String> original = new List<String>{'Ada', 'Grace'};
List<String> copy = original.clone();
copy.add('Linus');
System.debug(original.size()); // 2
System.debug(copy.size()); // 3Set and Map also have clone(). The copy is shallow. The new collection holds the same element references as the old one. For primitives like String or Integer this makes no difference, because they cannot be changed in place. For SObjects it matters a great deal.
The SObject trap
If a list holds SObjects, a shallow clone gives you two lists that contain the same record objects. Changing a field through one list changes it in the other.
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme')
};
List<Account> shallow = accounts.clone();
shallow[0].Name = 'Changed';
System.debug(accounts[0].Name); // ChangedMaking a deep copy with deepClone
List.deepClone() copies the list and also creates a new copy of each SObject in it. Edits to a record in the new list do not touch the original records.
List<Account> accounts = new List<Account>{
new Account(Name = 'Acme')
};
List<Account> deep = accounts.deepClone();
deep[0].Name = 'Changed';
System.debug(accounts[0].Name); // AcmeBy default, deepClone() does not keep record Ids. It has optional arguments to preserve Ids, preserve read-only timestamp and audit fields, and preserve auto-number fields. If you need an exact copy of queried records, pass true for the first argument:
List<Account> exact = accounts.deepClone(true);A copy made without Ids is useful when you want to insert duplicates of existing records.
Choosing a copy method
| Goal | Use |
|---|---|
| Second name for the same collection | Assignment |
| Independent list of primitives | `clone()` |
| Independent list of SObjects you will edit | `deepClone()` |
| Independent copy of a Map or Set | `clone()` |
Map has its own deepClone(), which also copies the values. Set has no deepClone().
Exercise
A method receives a List<Contact> parameter named input, calls input.clone(), and sets LastName on the first contact in the clone. Does the caller's list show the new LastName? Why, and what would you change to prevent it?
Show answer
Yes, the caller's list shows it. clone() is shallow, so both lists hold references to the same Contact objects. Use input.deepClone() so the clone holds separate Contact copies.