32

I've built a few apex controllers, and like most people have copied some code from here and there. In the controllers, there's typically an expression that looks like the following:

public *var type* someName {get; set;}

Initially I thought I needed to supply a function to the get part if the VF page was grabbing data, something like so:

get = getData(); set;

public function string getData(){
    return 'data!';
}

And the opposite for set (if users were inputting data). But now I see my controllers work perfectly well just having the top expression as {get;set;}.

I've looked through the salesforce docs and I've read all the parts regarding creating custom controllers - I see the get; set; being referenced, but nowhere explaining what this part does and how I can use it.

Have you come across any docs on what this part does and how I can use it?

Thank you all!

Zac

5 Answers 5

48

Visualforce requires a "getter" and "setter" to reference a variable in the controller or extension. Without a getter or setter, even public or global variables cannot be referenced in Visualforce expressions.

"get;" is basically: public *datatype* getVarName() { return varName; }

"set;" is basically: public void setVarName(*datatype* value) { varName = value; }

There are subtle differences though. For example, the function doesn't actually exist; it's called implicitly.

You can make variables read-only by not including set;, and you can make them write-only by not including get;.

Inside Apex Code, you can also use these to control readability and writeability.

For example:

public static Integer counter {
    get { if(counter == null) counter = 0; counter++; return counter; }
    private set; }

This counter increases by 1 each time it's used, but can't be set outside of the class that contains it.

public class OtherClass {
   public void someMethod() {
       System.debug(FirstClass.counter);
       System.debug(FirstClass.counter);
       FirstClass.counter = 10; // Compiler error
   }
}

Without the compiler error, it would debug 0, then 1.

0
19

What you are seeing with {get;set;} is an example of automatic properties.

From the Apex Documentation on Automatic Properties.

Properties do not require additional code in their get or set accessor code blocks. Instead, you can leave get and set accessor code blocks empty to define an automatic property. Automatic properties allow you to write more compact code that is easier to debug and maintain. They can be declared as read-only, read-write, or write-only. The following example creates three automatic properties:

Here is the example from the docs

public class AutomaticProperty {
   public integer MyReadOnlyProp { get; }
   public double MyReadWriteProp { get; set; }
   public string MyWriteOnlyProp { set; }
}

You could just as easily do something like this instead, but using the automatic properties saves a lot of time and coding.

public class nonAutomaticProperty {
   public integer MyReadOnlyProp { 
        public get {return MyReadOnlyProp;} 
        private set;
   }
   public double MyReadWriteProp { 
        public get {return MyReadWriteProp;} 
        public set; 
   }
   public string MyWriteOnlyProp { 
        private get {return MyWriteOnlyProp;} 
        public set; 
   }
}
1
  • +1 for adding the link to the documentation, as requested in the question. Feb 21, 2019 at 12:57
8

This was answered on SF dev forums: Reg: Get and set Methods

That post linked to a great aticle on the behavior of the default get/set methods: Getter and setter methods - What are they??

1
  • Links should be accompanied by the critical information they provide.
    – isherwood
    Aug 17, 2022 at 14:07
5
public *var type* someName {
    get {
        // this block equals to: 
        //     public *var type* getSomeName()

        // work with someName     
        return someName; 
    }
    set {
        // this block equals to: 
        //     public void setSomeName(*var type* value)

        // work with value
        someName = value;
    }
}
0
5

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.

You must log in to answer this question.

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