Sub topic 8
Constants with final
This chapter assumes you know how to declare Apex variables and use basic types such as Integer, String, and List. It also assumes you have run anonymous Apex in the Developer Console or an IDE.
What final does
The final keyword makes a variable assignable only once. After the first assignment, any attempt to change the variable is rejected. Apex has no separate const keyword, so final is how you declare a constant.
final Integer MAX_RETRIES = 3;
Integer attempts = 0;
attempts += 1;
System.debug(attempts < MAX_RETRIES);attempts changes freely. MAX_RETRIES cannot. Reassigning it, as in MAX_RETRIES = 5;, fails at compile time, so the error appears before any code runs.
Where you can assign the value
A final variable must be assigned exactly once. Where that assignment may happen depends on the kind of variable.
| Kind of variable | When it can be assigned |
|---|---|
| Local variable | At declaration, or once later in the same method |
| Member variable | At declaration, in an initializer block, or in a constructor |
| Static final variable | At declaration or in a static initializer block |
Static final variables are the usual way to share a constant across a class:
public class RetryConfig {
public static final Integer MAX_RETRIES = 3;
public static final String DEFAULT_STATUS = 'Pending';
}Other code reads the constant as RetryConfig.MAX_RETRIES. Defining it once removes repeated "magic numbers" and gives you a single place to change it.
Final does not freeze the object
final fixes the reference, not the contents. A final List cannot be pointed at a different list, but you can still add to it and remove from it.
final List<String> names = new List<String>{ 'Ada' };
names.add('Grace');
System.debug(names.size());This prints 2. Writing names = new List<String>(); would fail to compile. The same applies to Sets, Maps, and SObjects. A final Account can still have its fields changed.
Common errors
- Assigning a final variable a second time, including inside a loop body.
- Declaring a final local variable and never assigning it. Give it a value at declaration.
- Assuming a final collection is immutable. Only the reference is locked.
- Using a lowercase name. Apex does not require it, but the convention is uppercase with underscores for constants such as
MAX_RETRIES, which makes them easy to spot.
Exercise
Given final List<Integer> scores = new List<Integer>{ 10, 20 };, which of these statements compile: scores.add(30); and scores = new List<Integer>();? What does scores.size() return after the statements that compile?
Show answer
Only scores.add(30); compiles. final locks the variable's reference, so scores = new List<Integer>(); is rejected. The add changes the list's contents, which final allows, so scores.size() returns 3.
Exercise
Write a class TaxConfig that exposes a constant RATE of type Decimal with the value 0.075, readable from other classes as TaxConfig.RATE.
Show answer
public class TaxConfig { public static final Decimal RATE = 0.075; }