GETTERS AND SETTERS - WHAT ARE THEY? | Techila
858
single,single-post,postid-858,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

GETTERS AND SETTERS – WHAT ARE THEY?

DATA LOADER IN SALESFORCE

GETTERS AND SETTERS – WHAT ARE THEY?

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:

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


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

 

Courtesy: http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html

Author: techila

No Comments

Post A Comment