Getter and setter methods - What are they??
9:18 AMWell, 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
Thanks for this this was very easy to understand the get and set thanks a lot
ReplyDeleteThanks a lot for your comments. Your comment really encourages me to post more...
ReplyDeleteHi there.
ReplyDeleteThis really helped me a lot!!
I was able to fix my issue by reading your blog!
Thanks a lot ... that really keeps me motivated to write more..
ReplyDeleteThis 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.
ReplyDeleteThanks.
This post gave me a clear idea of get set. Thank you and keep posting.
ReplyDeleteThanks this helped me a lot!
ReplyDeletehi it is very clear same code in the visual force tutorial is not clear thank u
ReplyDeleteHi this Information is use full to learn GET SET methods. Thanks for providing this information.
ReplyDeleteI don't think this works - since there is apparently no longer any way to add or edit Apex code.
ReplyDeleteplease could you help
ReplyDeleteHi.. If you are not able to add/edit apex code it depends upon your Edition of Salesforce..What edition do you use?
ReplyDeletethank you very much...now the concept is clear to me...
ReplyDeleteThanks for the post.
ReplyDeletereally helpful, keep up the good work
ReplyDeleteGreat very simple and clear it's really good for beginner May u help me on Test Class . I am beginner in Apex
ReplyDeleteThank you.. I have started writing a guide for TEST METHODS.. I have only the first two parts ready now.. Here it is http://www.forcetree.com/2011/01/guide-to-writing-test-methods-for-apex.html- PART 1 and....
ReplyDeletehttp://www.forcetree.com/2011/06/guide-to-writing-test-methods-for-apex.html - PART 2
great post
ReplyDeleteclear in explaining getter and setter methods
ReplyDeleteThanks for this post. I have a question - how many custom controllers can a VF page have? Can i use multiple custom controllers?
ReplyDeleteExcellent.
ReplyDeletethank you.
Thank you very much for such a providing good knowledge.its very helpful for who is starting career in salesforce..
ReplyDeleteHi
ReplyDeleteCan any provide step by step approach to learn dynamic apex(as u do for setter and getter methods)
Thanks,
tnx dude
ReplyDeletegreat explaination, thanks!!!!
ReplyDeletethanx a lot
ReplyDeletennnnnnnnnnnnn
ReplyDeletegreat explanation !!!!!!!!!!
ReplyDeleteFor the last explanation why not add the userinput variable?
ReplyDeletepublic class simplegetset
{
public String userinput;
public String getuserinput(){return userinput;}
public void setuserinput(String userinput)
{
this.userinput = userinput;
}
}
Now it's functional
Really Helpful
ReplyDeleteYour post helps me lot. Thank you very much.
ReplyDeletereally useful thanks.....could you please also write on trigger.old and trigger.old.map..?
ReplyDeleteKeep up with the good work bro :) You're awesome.
ReplyDeleteMex
Awesome dude...it made my day easy
ReplyDeleteVery Helpful post.....
ReplyDeleteNice post ...good for freshers like me
ReplyDeletegreat work done ..... Thanks a lot . .. There is no such book i have founded .. where it is do much easy to learn ..
ReplyDeleteAnks
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.
ReplyDeleteTHANKU FOR YOU GIVING BASIC IDEAS FOR SET GET METHODS
ReplyDeleteAND GIVE ME US TO TRIGGER.OLD, TRIGGER.NEW
thanks alot for giving basic ideas. please also explain what action the action function and rerender will do in this example.
ReplyDeleteVery nice.
ReplyDeleteThanks !!
Excellent Explanation ! Thanks a Lot !
ReplyDeleteGot 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
Superb..!!!
ReplyDeleteThanks a bunch.
ReplyDeletethanks
ReplyDeleteThat was really a good way of explaining. Thanks for the explanation
ReplyDeleteThat was the best way of explaining get and set. Thanks !!!!
ReplyDeleteThanks a lot...:)
ReplyDeletethanku
ReplyDeleteThank you very much !!!!!!!!!!!!!!!!!!!!!!
ReplyDeleteIn 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?
ReplyDeletepublic void setuserinput(String userinput)
Delete{
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 :)
for me the the last piece of code is working without actionSupport attribute.Can you help why?
ReplyDeletegood
ReplyDeleteIs it mandatory to use a getter(get) method before using any setter(set) method?
ReplyDeleteSomething that is discovered.
ReplyDeleteIf 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?
Thanks a lot to post this get set concept here. I was able to connect with this and it solved my problem.
ReplyDeleteThis is just simple and clear picture of what these methods are doing...Thanks a lot
ReplyDeletevery well explained.
ReplyDeleteThank you.
Thank you very much. It's so clearly now.
ReplyDeletethank 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