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 (i.e. basically your controller or extension)
So go ahead and create a simple Visualforce page:
The Apex code for this page:
Now, the variable “userinput” in your Apex class will store the value entered by the user….
Let’s analyze the methods now…
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.
You can now see that your page loads with a value ‘John’…
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..
The Apex code would be…
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.
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”.