This excerpt is taken from Learning Apex Programming, by Matt Kaufman.
In order for an Apex class to function as a controller for a Visualforce page, the
variables and methods in the class need to use the correct syntax. The variables in the
class are made accessible to the page through the use of getter and setter methods.
A getter method is a nonstatic method that has no input parameters and returns an
object. Visualforce pages use getter methods to get objects from your custom controller.
A setter method is a nonstatic method that has only one input parameter and does not
return anything. Visualforce pages use setter methods to set the value of an object in
your custom controller. Let's take a look at some getter and setter methods:
//First declare your variable
Account myAccount;
//Here is the getter
public Account getMyAccount(){
//We don't want to return a null value
if ( myAccount == null ){
myAccount = new Account();
}
return myAccount;
}
//Here is the setter
public void setMyAccount(Account a){
myAccount = a;
}
Pretty neat, right? Just by naming our method getX or setX and using the right
signature, they are automatically accessible by our Visualforce page. This just gets
better; our getter and setter are using a whole record and our page has access to all
of the fields on that record. We can even use a collection of records and have access
to all of the records in the collection and all of their fields. In fact, your getters and
setters can work with any object and your Visualforce page automatically has access
to all of the instance attributes and action methods on that object.
Getters and setters seem simple, but the previous example really seems like a lot
of code to just allow our Visualforce page to work with a single object. This is why
Apex also includes a shortcut for getters and setters called properties. A property
combines the instance of our variable with the getter and setter methods and greatly
reduces the amount of code you need to write. Take a look at the property equivalent
to the previous code block:
//Declare your property and you're done
Account myAccount {get;set;}
Yes, we are serious that one line of code replaces the variable and the getter and setter
methods. It really doesn't get more efficient than that! Now, if you've been paying
attention, you will see a flaw in our property. If our myAccount variable is null, our
Visualforce page can get a nasty error. Our getter took care of that possibility, but our
property does not. There are two options to get around this. Let's look at the first option:
//Declare your property with logic and you're done
Account myAccount {
get{
if ( myAccount == null ){
myAccount = new Account();
}
}
set;
}
In this property, we expanded the getter to include our logic. This combines the
getter method with the property concept. Although perfectly legitimate, the second
option is actually the most common one, and this is to set the value of your attributes
in your class' constructor, as shown in the following lines of code:
public class myCustomController{
//Here's our property
Account myAccount {get;set;}
//Here's our constructor
public myCustomController(){
myAccount = new Account();
}
}
The key to this code block is in knowing that constructors are called prior to getters.
This allows us to execute our code before the Visualforce page communicates with
our controller. This pattern of using properties and setting their initial state in the
constructor is very common and uses a lot less code. In fact, most classes used with
Visualforce have a whole list of properties at the top of the class.