Getter and setter methods - What are they??

9:18 AM

Well, once you start using a controller or an extension you will get used to these words...

So, it is good that we understand what these methods really do..

Getter and setter methods are used to pass data from your visualforce page to your controller and vice versa..

Let's take a very simple scenario... Let's assume you want to display a textbox in your visualforce page.. When the user enters some value in this textbox and clicks on a button you want the value entered by the user in your Apex class (ie. basically your controller or extension)

So go ahead and create a simple visualforce page.. the code for this would be

<apex:page controller="simplegetset">
<apex:form>
<apex:outputlabel value="Enter your name here"/>
<apex:inputtext value="{!userinput}"/>
</apex:form>
</apex:page>


The Apex code for this page would be...

public class simplegetset
{
public String userinput{get; set;}
}


Now, the variable "userinput" in your Apex class will store the value entered by the user....

Let's analyze the methods now...

Get

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...

So, lets modify our code and pass a default value to the textbox.. Change the Apex code as follows..

public class simplegetset
{
public String userinput;
public String getuserinput(){return 'John';}

public void setuserinput(String userinput)
{
this.userinput = userinput;
}
}


You can now see that your page loads with a value 'John'...

Set

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..

Now modify your VF page as below..

<apex:page controller="simplegetset">
<apex:form>
<apex:outputlabel value="Enter your name here"/>
<apex:inputtext value="{!userinput}">
<apex:actionsupport event="onclick" rerender="display" />
</apex:inputtext>
<apex:outputpanel id="display">
<apex:outputtext value="The name entered is {!userinput}"/>
</apex:outputpanel>
</apex:form>
</apex:page>


The Apex code would be...

public class simplegetset
{
public String userinput{get; set;}
}


In this example what happens is.. the variable "userinput" stores the value entered in visualforce page and passes it to the Apex code.. hence you are able to see the entered value in the visualforce page..

I guess you might understand what i am saying.. to make things simple now use the same visualforce page.. but modify the Apex code as below..

public class simplegetset
{
public String userinput;
public String getuserinput(){return 'John';}

public void setuserinput(String userinput)
{
this.userinput = userinput;
}
}


Now, whatever value you enter the page always displays "The name entered is John".... This is because your get method always returns 'John'... but still your set method will store the value you entered in the variable "userinput"....


Hope i did not confuse...

62 comments

  1. Thanks for this this was very easy to understand the get and set thanks a lot

    ReplyDelete
  2. Thanks a lot for your comments. Your comment really encourages me to post more...

    ReplyDelete
  3. Hi there.

    This really helped me a lot!!

    I was able to fix my issue by reading your blog!

    ReplyDelete
  4. Thanks a lot ... that really keeps me motivated to write more..

    ReplyDelete
  5. This really for dummy and it helped me a lot. I hope I can see tutorial like this, explaining in detail just like explaining to a grade one pupil.

    Thanks.

    ReplyDelete
  6. This post gave me a clear idea of get set. Thank you and keep posting.

    ReplyDelete
  7. hi it is very clear same code in the visual force tutorial is not clear thank u

    ReplyDelete
  8. Hi this Information is use full to learn GET SET methods. Thanks for providing this information.

    ReplyDelete
  9. I don't think this works - since there is apparently no longer any way to add or edit Apex code.

    ReplyDelete
  10. please could you help

    ReplyDelete
  11. Hi.. If you are not able to add/edit apex code it depends upon your Edition of Salesforce..What edition do you use?

    ReplyDelete
  12. thank you very much...now the concept is clear to me...

    ReplyDelete
  13. really helpful, keep up the good work

    ReplyDelete
  14. Great very simple and clear it's really good for beginner May u help me on Test Class . I am beginner in Apex

    ReplyDelete
  15. clear in explaining getter and setter methods

    ReplyDelete
  16. Thanks for this post. I have a question - how many custom controllers can a VF page have? Can i use multiple custom controllers?

    ReplyDelete
  17. Excellent.
    thank you.

    ReplyDelete
  18. Thank you very much for such a providing good knowledge.its very helpful for who is starting career in salesforce..

    ReplyDelete
  19. Hi
    Can any provide step by step approach to learn dynamic apex(as u do for setter and getter methods)

    Thanks,

    ReplyDelete
  20. great explaination, thanks!!!!

    ReplyDelete
  21. For the last explanation why not add the userinput variable?

    public class simplegetset
    {
    public String userinput;
    public String getuserinput(){return userinput;}

    public void setuserinput(String userinput)
    {
    this.userinput = userinput;
    }
    }

    Now it's functional

    ReplyDelete
  22. Your post helps me lot. Thank you very much.

    ReplyDelete
  23. really useful thanks.....could you please also write on trigger.old and trigger.old.map..?

    ReplyDelete
  24. Keep up with the good work bro :) You're awesome.

    Mex

    ReplyDelete
  25. Awesome dude...it made my day easy

    ReplyDelete
  26. Very Helpful post.....

    ReplyDelete
  27. Nice post ...good for freshers like me

    ReplyDelete
  28. great work done ..... Thanks a lot . .. There is no such book i have founded .. where it is do much easy to learn ..


    Anks

    ReplyDelete
  29. Great work...If possible please share output VF screen also, Please share links for your other posts related to VF and Apex which will help in learning for beginners like me.

    ReplyDelete
  30. THANKU FOR YOU GIVING BASIC IDEAS FOR SET GET METHODS
    AND GIVE ME US TO TRIGGER.OLD, TRIGGER.NEW

    ReplyDelete
  31. thanks alot for giving basic ideas. please also explain what action the action function and rerender will do in this example.

    ReplyDelete
  32. Very nice.
    Thanks !!

    ReplyDelete
  33. Excellent Explanation ! Thanks a Lot !

    ReplyDelete



  34. Got the details explained, but it doesnt work when i want to allow user entered value to be updated in Phone#.

    How to write the get set for this?? Thanks

    ReplyDelete
  35. That was really a good way of explaining. Thanks for the explanation

    ReplyDelete
  36. That was the best way of explaining get and set. Thanks !!!!

    ReplyDelete
  37. Thank you very much !!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete
  38. In the modified example whatever is e given in the vf page will be passed to controller.but what exactly is meant by this.userinput = userinput;? what are you trying to acheive here?

    ReplyDelete
    Replies
    1. public void setuserinput(String userinput)
      {
      this.userinput /** see 1 below **/ = userinput /** see 2 below **/;
      }

      1) this.userinput : This is userinput variable of type string which is defined in the controller at the top. "this" keyword is used to point towards the local variables of the class (in our case userinput) and hence its "this.userinput"


      2) userinput: is the parameter that which is accepted by setuserinput method as parameter.

      Basically the author is assigning the parameter to local variable of the controller there by setting it.

      Hope this helps :)

      Delete
  39. for me the the last piece of code is working without actionSupport attribute.Can you help why?

    ReplyDelete
  40. Is it mandatory to use a getter(get) method before using any setter(set) method?

    ReplyDelete
  41. Something that is discovered.
    If I sue public String consoleId { get; set;} then a system.debug to check the value it works.

    However if I do the following with the with the same system.debug but adding a system.debug inside the GET and SET all my debug line are NULL.
    public String consoleId { get{System.debug('GET myString: '+consoleId);
    return consoleId;}
    set{System.debug('SET myString: ' + consoleId);}
    }
    Would you know any reason for it?

    ReplyDelete
  42. Thanks a lot to post this get set concept here. I was able to connect with this and it solved my problem.

    ReplyDelete
  43. This is just simple and clear picture of what these methods are doing...Thanks a lot

    ReplyDelete
  44. Thank you very much. It's so clearly now.

    ReplyDelete
  45. thank you. I always confuse simply because the "get" and "set" meaning. In English get mean taking sth, but in programming language it "give" value to the data in VF page rather than "taking" from it. So confusing

    ReplyDelete