Activity Forums Salesforce® Discussions What is the purpose of using Getter and Setter methods in salesforce?

  • PRANAV

    Member
    February 14, 2018 at 8:14 am

    Hi Ankit,

    Visualforce requires a "getter" and "setter" to reference a variable in the controller or extension. Without a getter or setter, even public or global variables cannot be referenced in Visualforce expressions.

    Get methods are used to initialize a property(variable) in a class such that when a visualforce page calls that property - it can display its value on the vf page

    The "get" method is used to pass data from your Apex code to your Visualforce page.. In our example we are not passing any value.. hence, when your page loads initially the textbox will have a empty value.

    "get;" is basically: public *datatype* getVarName() { return varName; }

     

    Set methods are used to assign a new value to the property.

    The "set" method is used to pass values from your visualforce page to the controller... In our example the variable "userinput" will be storing the value entered in the textbox.

    "set;" is basically: public void setVarName(*datatype* value) { varName = value; }

    Hope this helps you.

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos