3

I am new to Visualforce and trying to understand how getters and setters work.I am taking baby steps and have a simple page to start with -

<apex:page controller="GetterSetterCon">
    string=  {!str} <br/> 
    boolean= {!boo} 
</apex:page>

and the controller -

public class GetterSetterCon {

    public string str{get;set;}

    public boolean boo{get;set;}
}

The result I see on the page is -

string=null and boolean= false

Why defining a get accessor on the property makes boo to false?

| improve this question | | | | |
5

Visualforce performs some coercion on values retrieved from getters to avoid certain error conditions. For example, consider this expression:

<apex:outputText value="Hello World" rendered="{!boo}" />

If boo (as above), were allowed to return null, an error would happen. Try rendering this in Apex Code to see what happens:

// Page constructor in Apex Code
public GetterSetterCon() {
    if(boo) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Hello World'));
    }
}

Note that this code crashes on page load with a null pointer exception, even though boo returns false in your example.

Visualforce is generally a bit more type-safe than Apex Code is, although you should still be aware of trying to render non-existent map keys.

You won't see this in the debug logs, because it doesn't occur in a way that's visible to the developers. Null renders as "nothing" in most cases, but renders as false in the case of boolean.

Running iterators, like apex:repeat, across a null list won't crash the page either; it treats it as if it were a simple empty list.

Perhaps the most important piece of advice I can give to a new developer is to never leave variables uninitialized. Other languages tend to either provide default values for uninitialized values, or provide compile-time errors. Apex Code and Visualforce do neither (generally, but there are exceptions), so you should always make sure you've initialized your variables. Special exceptions to default initialization are noted in the documentation.

| improve this answer | | | | |
  • Thanks Brian for the wonderful explanation and advice for moving forward. I am assuming this coercion/behaviour by visualforce should also extend to associated controllers on custom components. I find few explanations in visualforce guide very confusing which led me to ask - salesforce.stackexchange.com/q/69279/12753. Here the constructor on associated custom component controller has already executed but still the doc says the value of a <classvariable> is unknown etc. – Jarvis Mar 16 '15 at 19:44
0

Getter methods are called after the constructors. Any variable initialized with some value in the constructor will be overridden by the getter method if explicitly specified.

String searchText;


public String getSearchText() {
    return searchText;
}

public void setSearchText(String s) {
    searchText = s;
}



VF PAGE:
apex:inputText id="searchText" value="{!searchText}"

whatever you write in the searchtext (inputtext), it is automatically passed to the setter method and then returned by getter. The same is achieved if you use get;set; instead of getter setter methods.

| improve this answer | | | | |
  • I don't find your answer relevant. – Jarvis Mar 16 '15 at 21:06
  • The point is, either set this boolean using a check box in your page OR set true/false in the class constructor. Dont leave this uninitialized. – the_phantom Mar 16 '15 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.