Quantcast
Channel: JAVA – Jitendra Zaa's Blog
Viewing all articles
Browse latest Browse all 12

Pass by Value and Pass by Reference

$
0
0

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 also.

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.

Question: parameters in method are passed by value or pass by reference?
Maximum answers: primitive data types like Integer, Double are pass by value and Objects are pass by reference.
However, the above statement is not perfect.

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.
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.

Scenario 1: Change field values
Try to run below Apex code in developer console or in Java:

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';
}
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”. 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.

Scenario 2 : Trying to change object
Run below code in developer console or in Java

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';
}

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()”. The parameter is actually passed “by value”, where the copy of value “a” is passed to object “b”. As we can see that both variable had reference of same object initially. However, inside the method new object is created and assigned to “b” and here problem occurs. 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.
So, finally we can say that everything is “pass by value”.

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.”

I hope this article will help to clarify between “pass by value” and “pass by reference”.


Viewing all articles
Browse latest Browse all 12

Trending Articles