Activity Forums Salesforce® Discussions How to pass parameters by reference and by value in Salesforce Apex?

  • madhulika shah

    Member
    July 26, 2018 at 11:52 am

    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:

    https://developer.salesforce.com/blogs/developer-relations/2012/05/passing-parameters-by-reference-and-by-value-in-apex.html

     

  • shariq

    Member
    September 19, 2018 at 1:24 am

    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.

Popular Salesforce Blogs

Popular Salesforce Videos