Activity › Forums › Salesforce® Discussions › How to pass parameters by reference and by value in Salesforce Apex?
Tagged: Memory Allocation, Parameter, Pass By Reference, Pass By Value, Salesforce Apex, Variables and Methods
-
How to pass parameters by reference and by value in Salesforce Apex?
Posted by shradha jain on July 26, 2018 at 11:33 AMHow to pass parameters by reference and by value in Salesforce Apex?
shariq replied 7 years, 7 months ago 3 Members · 2 Replies -
2 Replies
-
Hi Shradha,
Referring to passing parameters in salesforce first I want to define what actually pass by reference and pass by value means.
“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.
For passing parameters by value and by reference in apex you can refer to the following link:
- [adinserter block='9']
-
Hi,
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.
“Pass by reference” means when a method is called, that actual variable is passed to the method
Hope this helps.
Log In to reply.