function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mamidi maheshmamidi mahesh 

execution of getter methods in apex controller class

my requirement is that i have to get the one value from visualforce page and i have to set that value to instance variable. instance variable is
public String sscradio;
my code is below

public class MyController{
public String sscradio;
private final Candidate__c candRec;
public MyController(ApexPages.StandardController stdController){

this.candRec=(Candidate__c)stdController.getRecord();
System.debug(' we are in mycontroller constructor');
}
public String getSscradio(){
System.debug(' we are in mycontroller getter method');
return sscradio;
}
public void setSscradio(String sscradio){
System.debug(' we are in mycontroller setter method');
this.sscradio=sscradio;
}
public void submit(){
if(sscradio=='Yes'){
candRec.SSC_Certificate_submitted__c='Yes';
}else{
candRec.SSC_Certificate_submitted__c='No';
}
System.debug(' we are in mycontroller submit()'); 
update candRec;
}


when i save the vf page object is creating for controller class Mycontroller. and after saving if i go and see the debug logs, first constructor is executed,after that getter method is executed. when we click on submit in vf page  then again getter method is being executed after that setter method is executed after that submit() is executed, after that again getter method is executed. why getter method is being executed many times for one instance variable.
vf page conde is below


<apex:page standardController="Candidate__c" extensions="MyController"> <apex:form > <apex:pageBlock title="Survey form"> <apex:pageBlockSection title="Certificate Details" columns="1"> <apex:selectRadio label="SSC Certificate Submitted?" value="{!sscradio}"> <apex:selectOption itemlabel="Yes" itemvalue="Yes"/> <apex:selectOption itemLabel="No" itemvalue="No"/> </apex:selectRadio> </apex:pageBlockSection> </apex:pageBlock> <apex:commandButton value="submit" action="{!submit}"/> </apex:form> </apex:page>
please could any one explain that why getter method is being executed many times for one instance variable.
James LoghryJames Loghry
In short, a getter is accessed any time visualforce (or in some cases apex) needs to read a variable.  Setters are called any time a variable is updated, such as through a commandButton or action call.  Rerendering a Visualforce page?  Getters are called again.  Hence, why it's always a good idea to keep your getters and setters very simple.  For more info, see this StackExchange thread: http://salesforce.stackexchange.com/questions/9167/getter-setter-confused