1

I have VF page

<apex:page controller="CHECK">
<apex:form >
 <apex:inputText value="{!inputvalue}"/>
 <apex:outputText value="{!display}"/>
 <apex:commandButton value="Go"/>
 </apex:form>
</apex:page>

controller class is

public class CHECK {
public string inputvalue{get{display = 'hello'; return inputvalue;}set{display = 'Displayfrom'; inputvalue = value;}}
public string display{get;private set{display = 'Setdisplay';}}
}

Output of display is always setdisplay on page load and also when command button is pressed. Is it simply because 'display's setter is getting invoked last and that is overriding the value and may be at another time it could be the reverse order?

1 Answer 1

3

There is some decent information available on the Visualforce Page Execution Order you may find useful. In this particular situation, what you are doing is considered bad practice. From the Salesforce documentation:

It’s a best practice for setter methods to be idempotent, that is, to not have side effects. For example, don’t increment a variable, write a log message, or add a new record to the database. Visualforce doesn’t define the order in which setter methods are called, or how many times they might be called in the course of processing a request. Design your setter methods to produce the same outcome, whether they are called once or multiple times for a single page request.

Calling your getter on inputvalue shouldn't effect your display.

On top of that, when you are setting your display, you are actually calling the set method on display. More Salesforce documentation:

The code in a set accessor executes when the property is assigned a new value.

Apex properties don't work like a traditional getter or setter. When you actually write a getter/setter method, you can get around calling the getter or setter by directly calling the variable. When you call an Apex property with a getter and setter code block, you always call that getter and setter.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .