Apex Pass by Reference Pass by Value Examples

Pass by value – all primitive data type arguments, such as Integer or String, are passed into methods by value. This means that any changes to the arguments exist only within the scope of the method. When the method returns, the changes to the arguments are lost.

Non-primitive data type arguments, such as sObjects, are also passed into methods by value. This means that when the method returns, the passed-in argument still references the same object as before the method call, and can’t be changed to point to another object. However, the values of the object’s fields can be changed in the method.

Pass by reference means when a method is called, that actual variable is passed to the method

Pass by reference

A map with String key and List as value

 
Map<String, List<String>> mapOfLst = new Map<String, List<String>>();
mapOfLst.put('test', new List<String>{'1'});
System.debug('test 1 > ' + mapOfLst.get('test')); // (1)
List<String> lstOfMap = mapOfLst.get('test');
lstOfMap.add('2');
System.debug('test 2 > ' + mapOfLst.get('test')); // (1, 2)

Note we do not have to put the list back to the map, like this:
mapOfLst.put(‘test’, lstOfMap);

This is unnecessary as the List is passed by reference and we can just add the value to the list and will be available in the map when we do a get by key

public class StrUtil {
    private String str;
    
    public StrUtil(String str){
        this.str = str;
    }
    
    public void setStr(String str){
        this.str = str;
    }
}

Map<String, StrUtil> mapOfLst = new Map<String, StrUtil>();
mapOfLst.put('test', new StrUtil('1'));
System.debug('test 1 > ' + mapOfLst.get('test')); //StrUtil:[str=1]
StrUtil str = mapOfLst.get('test');
str.setStr('2');
System.debug('test 2 > ' + mapOfLst.get('test')); //StrUtil:[str=2]

Works same when we use objects no need to put the update object back to map as it is passed by reference

Pass by value

Map<String, String> mapOfLst = new Map<String, String>();
mapOfLst.put('test', '1');
System.debug('test 1 > ' + mapOfLst.get('test')); //1
String str = mapOfLst.get('test');
str = '2';
System.debug('test 2 > ' + mapOfLst.get('test')); //1

Pass by value will not update the value of the map and we need to put value back to map
mapOfLst.put(‘test’, str);

Account acc = [Select Id, Name from Account limit 1][0];

public static Account setAccountName(Account acc){
    acc.Name = 'Update Name';
    return acc;
}

System.debug('>> ' + setAccountName(acc)); //still same account but Name field was changed

SObject pass by value, we can update the SObject fields but still reference the same SObject

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: