+ Start a Discussion
Nidhi MalhotraNidhi Malhotra 

Why are get set method used?

Hi All,

Could you please help me to let me know why get and set is used in Salesforce Apex? And how is it used and how gettter and setter methods are defined and even the differences.

Thanks,
Nidhi
Best Answer chosen by Nidhi Malhotra
Sonam_SFDCSonam_SFDC
Hi,

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
Set methods are used to assign a new value to the property :

Read though the examples in the below doc :
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_methods.htm

All Answers

Bhawani SharmaBhawani Sharma
When you want to set some variables  from VF page to class, you will have to define setter for you variable. Getter is being used to get the varibale value from class to page.
public String tectV {get; set;}
This means, you can use this variable on page to get the value from controller as well as set value from page. This is similar to bean concept in java.
Ramu_SFDCRamu_SFDC
Hi, Get, Set methods are basically known as properties. These are mostly used when the data is to be passed on from VF page to Controller. Get method will fetch the information from controller and show it on the visualforce page. Set method will set the value that is being entered in VF page.

The below KB article will explain more about this

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_properties.htm
Sonam_SFDCSonam_SFDC
Hi,

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
Set methods are used to assign a new value to the property :

Read though the examples in the below doc :
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_methods.htm
This was selected as the best answer