function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gyanendra Singh 8Gyanendra Singh 8 

Getter and Setter behaviour

Below is a simple visualforce page which is self understood and the controller class:
VFPage----------

<apex:page controller="TestController1">
<apex:form >
<apex:pageBlock title="My Content" mode="edit">
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputText value="{!acc}"/>            
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller Class------------------

public class TestController1 {
public String S1;
public String acc {
get;
set{
S1='Hello'; }
 } }

Now I want to know this. When I comment out the line  S1='Hello'  then my getter works and returns whatever text I enter in the inputText field. But when I don't comment out S1='Hello'  or basically if I write any other statement/expression APART from the expression which sets some value to the Property acc (like acc='Wassup'. And in this case the getter returns Wassup or whatever I set the value of acc) the getter method does not return anything. Why is this behaviour?
This is purely for learning purpose.

Thank you
pconpcon
The class level variable account fetched during the constructor and then is passed to the VisualForce page by reference inside of getAccount.  The inputField then sets the Name variable on the class level account variable.  Then when the save method is called, all fields are "queried*" from the visual force page to get their current values. The updated account varaible is then updated in the database.  In this instance the only field that would be updated is the Name field since it is the only one made editiable on the VisualForce page.

* query isn't quite the right word, but it fits best.  It is more pulled from the state variable that is passed to the save method on the button click.