[SalesForce] When is set and get method called in apex class and whats the issue when we don’t use it

public class opportunityextension2{
    public List<opportunity> opplist{get; set;}

    public opportunityextension2(ApexPages.StandardSetController controller) {

    }

    public opportunityextension2(){
        opplist = [select name, id from opportunity];
    }

    public pagereference  exportbutton(){
        return page.Exportbuttonpage; 
    }

}

Can someone please tell me when the get and set method is called?
in the above code, if the set and get method is not provided then

 public opportunityextension2(){
     opplist = [select name, id from opportunity];
 } 

won't above code set opplist with name and id from opportunity. what if we don't use set and get what is the problem we get.
if anyone can clear me it is really appreciated.
Thanks

VF page

<apex:page standardController="Opportunity"  recordSetVar="Opportunities" extensions="opportunityextension2" >
<apex:form >
    <apex:pageblock >

        <apex:pageblockSection >

        <apex:commandButton value="Export to excel" action="{!exportbutton}" /> <br/>
            <apex:pageblocktable value="{!opportunities}" var="opp" rows="19">


                <apex:column value="{!opp.Name}"/>
                apex:column value="{!opp.ID}"/>

            </apex:pageblocktable>


        </apex:pageblockSection>
         <apex:commandButton value="First" action="{!First}" />
        <apex:commandButton value="Next" action="{!Next}" disabled="{!NOT(hasnext)}" />
        <apex:commandButton value="Previous" action="{!previous}" disabled="{!NOT(hasprevious)}" />
         <apex:commandButton value="Last" action="{!Last}" />

    </apex:pageblock>



 </apex:form>
</apex:page>

Best Answer

The get and set method are methods contained in an Apex Property https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_properties.htm.

The get method is called anytime that the property is read, and the set property is called anytime that the property is set. If you don't define either, they are considered Automatic Properties, where the get method returns the value and the set method sets the value.

Some interesting and useful features of an Apex Property (which can be read in the link above), is that you can define a value as only readable or only writeable by only specifying the get method for readable, or the set method for writeable.

Your code could therefore be rewritten like the following:

Public class opportunityextension2{

    public list<opportunity> opplist{
        get {
            return [select name, id from opportunity];
        }, set;
    }

    public opportunityextension2(ApexPages.StandardSetController controller) {

    }

    public pagereference  exportbutton(){
        return page.Exportbuttonpage; 
    }
}

This means that anytime the opplist property is accessed (or read), it will execute the query and return the results. Maybe this isn't the most efficient implementation, but it demonstrates the purpose of an Apex Property.

Related Topic