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 ?

1 Answer 1

4

You call the getter here:

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

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

2
  • 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, 2019 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, 2019 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .