Getter Block execution without VF reference





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















My understanding of the Getter Setter is that getter is invoked if the particular variable is being referenced in VF Page.



However for the following code I see that Getter is getting called even if no variable reference is on the page.



    public class EmbeddedTest
{
public list<account> lAcc{
get{
system.debug('INSIDE GETTER');
return null;
}
set;
}

public void checkChanges()
{
lAcc=[select id,name from account limit 10];
system.debug('ACCOUNT LOG GENERATED '+lAcc);
}
}


VF Page:



<apex:page controller="EmbeddedTest">
<apex:form >
<apex:commandButton action="{!checkChanges}" value="Click to change!"/>
</apex:form>
</apex:page>


On clicking the button I see the below logs(in order):




08:37:22.0 (14537986)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|SELECT
id, name FROM account LIMIT 10



08:37:22.0 (19987384)|SOQL_EXECUTE_END|[19]|Rows:10



08:37:22.0 (20434238)|USER_DEBUG|[3]|DEBUG|INSIDE GETTER



08:37:22.0 (20537127)|USER_DEBUG|[20]|DEBUG|ACCOUNT LOG GENERATED null




Why does the Getter get called and make the list(lAcc) null even without any VF reference ?










share|improve this question































    1















    My understanding of the Getter Setter is that getter is invoked if the particular variable is being referenced in VF Page.



    However for the following code I see that Getter is getting called even if no variable reference is on the page.



        public class EmbeddedTest
    {
    public list<account> lAcc{
    get{
    system.debug('INSIDE GETTER');
    return null;
    }
    set;
    }

    public void checkChanges()
    {
    lAcc=[select id,name from account limit 10];
    system.debug('ACCOUNT LOG GENERATED '+lAcc);
    }
    }


    VF Page:



    <apex:page controller="EmbeddedTest">
    <apex:form >
    <apex:commandButton action="{!checkChanges}" value="Click to change!"/>
    </apex:form>
    </apex:page>


    On clicking the button I see the below logs(in order):




    08:37:22.0 (14537986)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|SELECT
    id, name FROM account LIMIT 10



    08:37:22.0 (19987384)|SOQL_EXECUTE_END|[19]|Rows:10



    08:37:22.0 (20434238)|USER_DEBUG|[3]|DEBUG|INSIDE GETTER



    08:37:22.0 (20537127)|USER_DEBUG|[20]|DEBUG|ACCOUNT LOG GENERATED null




    Why does the Getter get called and make the list(lAcc) null even without any VF reference ?










    share|improve this question



























      1












      1








      1








      My understanding of the Getter Setter is that getter is invoked if the particular variable is being referenced in VF Page.



      However for the following code I see that Getter is getting called even if no variable reference is on the page.



          public class EmbeddedTest
      {
      public list<account> lAcc{
      get{
      system.debug('INSIDE GETTER');
      return null;
      }
      set;
      }

      public void checkChanges()
      {
      lAcc=[select id,name from account limit 10];
      system.debug('ACCOUNT LOG GENERATED '+lAcc);
      }
      }


      VF Page:



      <apex:page controller="EmbeddedTest">
      <apex:form >
      <apex:commandButton action="{!checkChanges}" value="Click to change!"/>
      </apex:form>
      </apex:page>


      On clicking the button I see the below logs(in order):




      08:37:22.0 (14537986)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|SELECT
      id, name FROM account LIMIT 10



      08:37:22.0 (19987384)|SOQL_EXECUTE_END|[19]|Rows:10



      08:37:22.0 (20434238)|USER_DEBUG|[3]|DEBUG|INSIDE GETTER



      08:37:22.0 (20537127)|USER_DEBUG|[20]|DEBUG|ACCOUNT LOG GENERATED null




      Why does the Getter get called and make the list(lAcc) null even without any VF reference ?










      share|improve this question
















      My understanding of the Getter Setter is that getter is invoked if the particular variable is being referenced in VF Page.



      However for the following code I see that Getter is getting called even if no variable reference is on the page.



          public class EmbeddedTest
      {
      public list<account> lAcc{
      get{
      system.debug('INSIDE GETTER');
      return null;
      }
      set;
      }

      public void checkChanges()
      {
      lAcc=[select id,name from account limit 10];
      system.debug('ACCOUNT LOG GENERATED '+lAcc);
      }
      }


      VF Page:



      <apex:page controller="EmbeddedTest">
      <apex:form >
      <apex:commandButton action="{!checkChanges}" value="Click to change!"/>
      </apex:form>
      </apex:page>


      On clicking the button I see the below logs(in order):




      08:37:22.0 (14537986)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|SELECT
      id, name FROM account LIMIT 10



      08:37:22.0 (19987384)|SOQL_EXECUTE_END|[19]|Rows:10



      08:37:22.0 (20434238)|USER_DEBUG|[3]|DEBUG|INSIDE GETTER



      08:37:22.0 (20537127)|USER_DEBUG|[20]|DEBUG|ACCOUNT LOG GENERATED null




      Why does the Getter get called and make the list(lAcc) null even without any VF reference ?







      apex attribute






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 1 at 18:07









      Adrian Larson

      110k19120256




      110k19120256










      asked Feb 1 at 17:09









      starhunterstarhunter

      389417




      389417






















          1 Answer
          1






          active

          oldest

          votes


















          4














          You call the getter here:



          system.debug('ACCOUNT LOG GENERATED '+lAcc);


          Accessing the attribute value of lAcc in any way calls the getter.






          share|improve this answer
























          • Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

            – starhunter
            Feb 1 at 18:39











          • It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

            – Adrian Larson
            Feb 1 at 18:47












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "459"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f248844%2fgetter-block-execution-without-vf-reference%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          You call the getter here:



          system.debug('ACCOUNT LOG GENERATED '+lAcc);


          Accessing the attribute value of lAcc in any way calls the getter.






          share|improve this answer
























          • Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

            – starhunter
            Feb 1 at 18:39











          • It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

            – Adrian Larson
            Feb 1 at 18:47
















          4














          You call the getter here:



          system.debug('ACCOUNT LOG GENERATED '+lAcc);


          Accessing the attribute value of lAcc in any way calls the getter.






          share|improve this answer
























          • Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

            – starhunter
            Feb 1 at 18:39











          • It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

            – Adrian Larson
            Feb 1 at 18:47














          4












          4








          4







          You call the getter here:



          system.debug('ACCOUNT LOG GENERATED '+lAcc);


          Accessing the attribute value of lAcc in any way calls the getter.






          share|improve this answer













          You call the getter here:



          system.debug('ACCOUNT LOG GENERATED '+lAcc);


          Accessing the attribute value of lAcc in any way calls the getter.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 at 17:11









          Adrian LarsonAdrian Larson

          110k19120256




          110k19120256













          • Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

            – starhunter
            Feb 1 at 18:39











          • It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

            – Adrian Larson
            Feb 1 at 18:47



















          • Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

            – starhunter
            Feb 1 at 18:39











          • It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

            – Adrian Larson
            Feb 1 at 18:47

















          Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

          – starhunter
          Feb 1 at 18:39





          Okay. So does it mean that manipulation of lAcc(eg, the SOQL result assignment) will have no bearing on the value of lAcc. It will always return what the getter block returns.

          – starhunter
          Feb 1 at 18:39













          It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

          – Adrian Larson
          Feb 1 at 18:47





          It will always return what the getter block returns, yes. Assigning a value only calls the setter and doesn't call the getter unless you try to access the variable.

          – Adrian Larson
          Feb 1 at 18:47


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Salesforce Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f248844%2fgetter-block-execution-without-vf-reference%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          WPF add header to Image with URL pettitions [duplicate]