{"id":2868,"date":"2012-05-25T16:29:08","date_gmt":"2012-05-25T10:59:08","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2868"},"modified":"2020-02-13T18:59:43","modified_gmt":"2020-02-13T23:59:43","slug":"pass-by-value-and-pass-by-reference","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/pass-by-value-and-pass-by-reference\/","title":{"rendered":"Method Parameters &#8211; Pass by Value or Pass by Reference"},"content":{"rendered":"\n<p class=\"justify\">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.<\/p>\n\n\n\n<p class=\"justify\">Before moving ahead let&#8217;s have a summary on what is <strong>pass by value<\/strong>\u009d and <strong>pass by reference<\/strong>.<\/p>\n\n\n\n<p class=\"justify\"><strong>Pass by value\u009d<\/strong> means when a method is called, a <strong>second copy<\/strong> 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 <strong>unchanged<\/strong>, and the changes to the copy are lost upon return.<\/p>\n\n\n\n<p class=\"justify\"><strong>Pass by reference\u009d<\/strong> means when a method is called, that <strong>actual<\/strong> variable is passed to the method.<\/p>\n\n\n\n<p class=\"justify\"><strong>Million dollar question: <\/strong>parameters in method are passed by value or pass by reference?<br><strong>Most of answers:<\/strong> primitive data types like Integer, Double are pass by value and Objects are pass by reference.<\/p>\n\n\n\n<p class=\"justify\"><br><strong>However, the above statement is not 100% true.<\/strong> I know I just invited <strong>controversy<\/strong> on this post. Be Patient with me and I would prove with actual example, feel free to run these examples in your Developer Console. <\/p>\n\n\n\n<p class=\"justify\"><strong>Correct Answer:<\/strong> <strong>parameters<\/strong> 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 <strong>cannot change the Object itself<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote justify is-layout-flow wp-block-quote-is-layout-flow\"><p>Take a pause here and think what I just said. I&#8217;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.<\/p><\/blockquote>\n\n\n\n<p class=\"justify\"><br>Let&#8217;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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nAccount a = new Account(); \na.Name = 'Shivasoft'; \na.Website = 'www.JitendraZaa.com'; \nchangeWebsite(a); \nSystem.debug('Website - '+a.Website); \n\npublic void changeWebsite(Account b) { \n    b.Website = 'www.salesforce.com'; \n}  \n<\/pre><\/div>\n\n\n<p>Output of above code is <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWebsite - www.salesforce.com\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">What just happened in Example 1 <\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Pass-by-Value-or-Pass-by-Reference-1.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Pass-by-Value-or-Pass-by-Reference-1-1024x328.png?resize=768%2C246&#038;ssl=1\" alt=\"Salesforce Pass by Value or Pass by Reference - Scenario 1\" class=\"wp-image-2869\" width=\"768\" height=\"246\"\/><\/a><figcaption>Salesforce Pass by Value or Pass by Reference &#8211; Scenario 1<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"justify\"><strong>Explanation:<\/strong><br>As per code snippet, the object <strong>a<\/strong>  is supplied in method <em>changeWebsite()<\/em>. The parameter is actually passed  <strong>by value<\/strong>\u009d, where the copy of value a  is passed to object b which is <strong>memory address (aka reference)<\/strong>.  As we can see that both variable have reference of same object and hence whatever changes done by variable <em> b<\/em> on that object it will be applicable for variable  <em>a<\/em> also.<\/p>\n\n\n\n<p>If you are still not convinced and think I&#8217;m wrong, wait for example 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nAccount a = new Account(); \na.Name = 'Shivasoft'; \na.Website = 'www.JitendraZaa.com'; \nchangeWebsite(a); \nSystem.debug('Website - '+a.Website); \n\npublic void changeWebsite(Account b) { \n    \/\/Create New Object and try to replace old \n    b = new Account();\n    b.Website = 'www.salesforce.com'; \n}  \n<\/pre><\/div>\n\n\n<p class=\"justify\">Before I go further and reveal answer, let&#8217;s think what would be output ?<\/p>\n\n\n\n<p class=\"justify\">If you assumed method parameters are passed by reference then output should be <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWebsite - www.salesforce.com\n<\/pre><\/div>\n\n\n<p>However actual output is <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nWebsite - www.JitendraZaa.com\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">What just happened in example 2 ?<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Pass-by-Value-or-Pass-by-Reference-2.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/Salesforce-Pass-by-Value-or-Pass-by-Reference-2-1024x341.png?resize=768%2C256&#038;ssl=1\" alt=\"Salesforce Pass by Value or Pass by Reference - Scenario 2\" class=\"wp-image-2870\" width=\"768\" height=\"256\"\/><\/a><figcaption>Salesforce Pass by Value or Pass by Reference &#8211; Scenario 2<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"justify\"><strong>Explanation:<\/strong><br>As per code snippet, the object <em>a<\/em>  is supplied in method <em>changeWebsite()<\/em>. Like I said, the parameter itself is actually passed <strong>by value<\/strong>\u009d, where the copy of value <em>a<\/em> is passed to object <em>b<\/em> 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 <em>b<\/em> <strong>and just now important event occurred<\/strong>. Refer above image for clear explanation<\/p>\n\n\n\n<p class=\"justify\">Previously b was pointing to old object, if it was really reference then it should impact object a. However now <em>a<\/em> and <em>b<\/em> variables point to different object. Whatever the changes done inside method, it will not be reflected outside as object <em>a<\/em> still have old reference. And hence after method execution when we check the value of object <em>a<\/em>, it is not affected because of method execution.<\/p>\n\n\n<p>Salesforce documentation says :<\/p>\n<blockquote>\n<p>&#8220;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.<br>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&#8217;t be changed to point to another object. However, the values of the object&#8217;s fields can be changed in the method.&#8221;\u009d<\/p>\n<\/blockquote>\n\n\n<h2 class=\"wp-block-heading\">Time for Best Practice<\/h2>\n\n\n\n<p class=\"justify\">Because of behavior of parameters are passed as a value, it is always best practice to return object back to calling method. <strong>JUST DON&#8217;T ASSUME<\/strong> that object is passed by reference and whatever happened inside method will reflect calling method.<\/p>\n\n\n\n<p>So code would become <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nAccount a = new Account(); \na.Name = 'Shivasoft'; \na.Website = 'www.JitendraZaa.com'; \na = changeWebsite(a); \nSystem.debug('Website - '+a.Website); \n\npublic Account changeWebsite(Account b) { \n    \/\/Create New Object and try to replace old \n    b = new Account();\n    b.Website = 'www.salesforce.com'; \n    return b ;\n}  \n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Difference between pass by Value and pass by Reference in Salesforce and other Object Oriented Programming language<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,3],"tags":[337,329],"class_list":["post-2868","post","type-post","status-publish","format-standard","hentry","category-apex","category-java","tag-apex","tag-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2643,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/passing-parameter-in-actionfunction-in-visualforce\/","url_meta":{"origin":2868,"position":0},"title":"Passing multiple Parameters in ActionFunction in Visualforce","author":"Jitendra","date":"January 15, 2012","format":false,"excerpt":"Example and Source code of multiple Parameters Parameter in ActionFunction in Visualforce - Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Passing Parameter in ActionFunction in Visualforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/01\/Passing-Parameter-in-ActionFunction-in-Visualforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3411,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-interview-question-salesforce-part-16\/","url_meta":{"origin":2868,"position":1},"title":"Apex Interview Question \u2013 Salesforce &#8211; Part 16","author":"Jitendra","date":"July 28, 2013","format":false,"excerpt":"151. Give Sample Code Snippet of Apex that that will show that how Parent and Child record can be inserted in Single Statement ? Ans : It can be done with help of External Id. 152 . Which SOQL statement can be used to get all records even from recycle\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4967,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-flow-interview-questions-for-admins-part-23\/","url_meta":{"origin":2868,"position":2},"title":"Salesforce Flow Interview Questions for admins &#8211; Part 23","author":"Jitendra","date":"December 7, 2015","format":false,"excerpt":"\ufeff Consider it interview questions or FAQs, However below are some high level information or gotchas related to Salesforce Flow. 221. How to create lookup field in Salesforce flow? Ans : There is no direct way to create a lookup field in flow but we can use workaround mentioned in\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Sample Salesforce flow using loop element","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/11\/Sample-Salesforce-flow-using-loop-element.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/11\/Sample-Salesforce-flow-using-loop-element.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/11\/Sample-Salesforce-flow-using-loop-element.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","url_meta":{"origin":2868,"position":3},"title":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex","author":"Jitendra","date":"November 27, 2011","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3112,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-13\/","url_meta":{"origin":2868,"position":4},"title":"Salesforce Interview Question \u2013 Part 13","author":"Jitendra","date":"September 25, 2012","format":false,"excerpt":"121 : Consider we have overall 90% code coverage however there is one class which have 0% code coverage. Can we still able to deploy that class on production? Ans : Yes. Minimum 1% required for every trigger and there is no such restriction for Apex class. 122 : How\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6274,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/enterprise-territory-management-auto-account-assignment-using-apex\/","url_meta":{"origin":2868,"position":5},"title":"Enterprise Territory Management &#8211; Auto Account Assignment using Apex","author":"Jitendra","date":"September 22, 2017","format":false,"excerpt":"Use Apex code to auto assign Accounts on basis of Enterprise Territory Assignment rules","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Enterprise Territory - Auto Account Assignment using Apex","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=700%2C400 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2868"}],"version-history":[{"count":3,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2868\/revisions"}],"predecessor-version":[{"id":6993,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2868\/revisions\/6993"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}