What is Getter and Setter function in salesforce


if you are programmer then probably you might have heard about the getter and setter methods in class but if you are new in programming language then might be you never heard about it, So talking in the context of the salesforce getter and setter methods syntax are borrowed from both java and .NET languages but what actually are getter and setter methods, In general or layman language if we talked about then

Getter Function:

This method use to retrieve the values from apex classes or controller to visualforce pages.

Setter Function:

This Method use to set the values or to pass the values from visualforce pages to the apex classes or controller.

If we talk about in the context of the salesforce then we can say that getter methods are use to retrieve the values from the apex classes while setter methods are use to set the values or to pass the values from visualforce pages to apex classes to look it in more detail let us take an example to understand this concept in more detail.

Let us take a simple visualforce page, see the code below.

Now let us take a simple controller example and see how controller will interact with the visualforce page

We can say that it is a setter and getter variable or better we can called it as an apex properties as you can see above i mention both get and set which make the variable both readable and writable, see some below points .

//1.) MyReadOnlyPro variable can only use for passing values from apex class (Controller) to visualforce page and it is read only
//2). MyReadWriteProp variable can be use for passing the value from apex class to visualforce page and vice versa.
//3). MyWriteOnlyProp variable can only be use for passing the values from visualforce page to apex.

if i use MyReadWriteProp for transferring data from apex class(controller) to visualforce page and visualforce page to controller then when we load the we you can see a blank value in the text field because no value has been return from apex code, see below picture for more details.

If we do little change to our apex properties then we can also return a default value from the get to our visualforce page , see the below code.

As you can see that we only return the value to the visualforce page so we don’t need any set so we omitted it if we do little more change in our coding and instead of default apex properties, we can use getter and setter function to return a default value from the apex code, see below code with little change.

As you can see in the above code i have taken a function getinpt() which is different from an apex properties, So using any of the last above two codes when you load the page you would get a default value in your text field as a ‘test’ , see below picture for more details.

To pass the data from visualforce page to apex controller we have to use setter function which set the data or bind the value to the variable in apex class and so that we can use that variable anywhere in the visualforce page but make sure we have to use rerender feature to display the same data back from apex to visualforce ,Please see some important below points which are necessary if we are using apex properties or getter and setter function.

  • The body of the get accessor is similar to that of a method. It must return a value of the property type. Executing the get accessor is the same as reading the value of the variable.
  • The get accessor must end in a return statement.
  • When you assign a value to the property, the set accessor is invoked with an argument that provides the new value.
  • When the set accessor is invoked, the system passes an implicit argument to the setter called value of the same data type as the property.

I hope it removes your confusion between getter and setter function as well as on apex properties, if you have any query please leave a comment below in the comment section and don’t forget to subscribe to my blog and my social networking pages, if you want to read more about apex properties or getter and setter function then you can check the below link.





Related Articles :

Speak Your Mind