Method Parameters – Pass by Value or Pass by Reference

Author posted by Jitendra on Posted on under category Categories Apex, JAVA and tagged as Tags , with 5 Comments on Method Parameters – Pass by Value or Pass by Reference

Difference between pass by Value and pass by Reference in Salesforce and other Object Oriented Programming language

This topic has always been part of discussion where the answer is not satisfactory and convinced to others. Using this article, I will try to explain the concept of parameter passing in Apex Method. This Complete topic is applicable for Java or other programming languages as well.

Before moving ahead let’s have a summary on what is pass by value and pass by reference.

Pass by value means when a method is called, a second copy of the parameter variable is made in memory, and this copy is passed to the method as a parameter. This copy can be modified in the method, but the original variable in the caller is unchanged, and the changes to the copy are lost upon return.

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

Million dollar question: parameters in method are passed by value or pass by reference?
Most of answers: primitive data types like Integer, Double are pass by value and Objects are pass by reference.


However, the above statement is not 100% true. I know I just invited controversy on this post. Be Patient with me and I would prove with actual example, feel free to run these examples in your Developer Console.

Correct Answer: parameters are passed by value in Apex as well as in Java. Where, in case of object the copy of reference is first made and then passed, therefore we can change the fields of object inside method however we cannot change the Object itself.

Take a pause here and think what I just said. I’m saying, new variable copy created which points to same Object. So, it seems reference but variable technically itself is storing value which is address or reference of object.


Let’s discuss this in more detail. I am going to explain two scenarios in which I will try to change the field value and in second example I will try to change Object itself.

Example 1

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
changeWebsite(a); 
System.debug('Website - '+a.Website); 

public void changeWebsite(Account b) { 
    b.Website = 'www.salesforce.com'; 
}  

Output of above code is

Website - www.salesforce.com

What just happened in Example 1

Salesforce Pass by Value or Pass by Reference - Scenario 1
Salesforce Pass by Value or Pass by Reference – Scenario 1

Explanation:
As per code snippet, the object a is supplied in method changeWebsite(). The parameter is actually passed by value, where the copy of value a is passed to object b which is memory address (aka reference). As we can see that both variable have reference of same object and hence whatever changes done by variable b on that object it will be applicable for variable a also.

If you are still not convinced and think I’m wrong, wait for example 2.

Example 2

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
changeWebsite(a); 
System.debug('Website - '+a.Website); 

public void changeWebsite(Account b) { 
    //Create New Object and try to replace old 
    b = new Account();
    b.Website = 'www.salesforce.com'; 
}  

Before I go further and reveal answer, let’s think what would be output ?

If you assumed method parameters are passed by reference then output should be

Website - www.salesforce.com

However actual output is

Website - www.JitendraZaa.com

What just happened in example 2 ?

Salesforce Pass by Value or Pass by Reference - Scenario 2
Salesforce Pass by Value or Pass by Reference – Scenario 2

Explanation:
As per code snippet, the object a is supplied in method changeWebsite(). Like I said, the parameter itself is actually passed by value, where the copy of value a is passed to object b which was reference. As we can see that both variable had reference of same object initially. However, inside the method new object is created and reference assigned to b and just now important event occurred. Refer above image for clear explanation

Previously b was pointing to old object, if it was really reference then it should impact object a. However now a and b variables point to different object. Whatever the changes done inside method, it will not be reflected outside as object a still have old reference. And hence after method execution when we check the value of object a, it is not affected because of method execution.

Salesforce documentation says :

“In Apex, 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.”

Time for Best Practice

Because of behavior of parameters are passed as a value, it is always best practice to return object back to calling method. JUST DON’T ASSUME that object is passed by reference and whatever happened inside method will reflect calling method.

So code would become

Account a = new Account(); 
a.Name = 'Shivasoft'; 
a.Website = 'www.JitendraZaa.com'; 
a = changeWebsite(a); 
System.debug('Website - '+a.Website); 

public Account changeWebsite(Account b) { 
    //Create New Object and try to replace old 
    b = new Account();
    b.Website = 'www.salesforce.com'; 
    return b ;
}  

Related posts

5 thoughts on “Method Parameters – Pass by Value or Pass by Reference”

  1. I think you have wrong understanding of pass by reference. A pass by reference sends a new reference to the method called which is pointing to the same object. So there becomes two reference pointing to same object. In your example when you are assigning new object to reference b then it is referencing to new object but a still refers to the old object.

Leave a Reply to RG Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.