Sub topic 9
Static and instance initialization blocks
This chapter assumes you know how to declare an Apex class, and that you know the difference between static and instance members. It also assumes you know that static final creates a constant. Here you will see how an initialization block runs setup code, and when it runs.
What an initialization block is
An initialization block is a pair of braces placed directly in the class body, outside any method. Apex has two kinds:
- A static initialization block starts with the
statickeyword. It runs once, when the class is first used in a transaction. - An instance initialization block has no keyword, only braces. It runs each time you create an object, before the constructor body.
Static blocks and constants
A static final variable can be assigned once, either on its declaration line or inside a static block. Use a block when one expression is not enough, for example when you need a loop or several statements to build the value.
public class Config {
public static final Map<String, Integer> LIMITS;
public static final Integer TOTAL;
static {
LIMITS = new Map<String, Integer>{
'small' => 10,
'large' => 1000
};
Integer sum = 0;
for (Integer v : LIMITS.values()) {
sum += v;
}
TOTAL = sum;
}
}
// Usage
System.debug(Config.LIMITS.get('small')); // 10
System.debug(Config.TOTAL); // 1010The first reference to Config loads the class and runs the block. Later references in the same transaction reuse the values. A new transaction loads the class again and runs the block again, because static state does not outlive the transaction.
Instance blocks and construction order
An instance block runs for every new object. The order is:
- Instance variable initializers and instance blocks, in the order they appear in the class.
- The body of the constructor.
public class Session {
private List<String> steps = new List<String>();
{
steps.add('instance block');
}
public Session() {
steps.add('constructor');
}
public List<String> getSteps() {
return steps;
}
}
// Usage
System.debug(new Session().getSteps()); // (instance block, constructor)The block is useful when several constructors need the same setup. Without it, you would repeat the code in each constructor or chain them with this(...). Chaining is usually clearer, so reach for an instance block only when the shared setup is small.
Common errors
- Assuming the static block runs at deploy time. It runs at runtime, on first use in each transaction.
- Assigning a
static finalvariable twice. You can assign it on the declaration or in the block, not both. - Relying on order across blocks. Static initializers and blocks run in the order written, so a block that reads a static variable declared further down sees
null. - Putting heavy work in a static block. It runs on every transaction that touches the class. A SOQL query there uses up governor limits even when the value is never needed.
Exercise
A class has the instance variable private Integer x = 1;, then an instance block that sets x = 2;, then a constructor that sets x = 3;. What value does x hold after new finishes? How many times does a static block in the same class run if you create five objects in one transaction?
Show answer
x is 3. The initializer sets 1, the block sets 2, and the constructor body runs last and sets 3. The static block runs once, because it runs on the first use of the class in the transaction and not for each object.