Sub topic 2
How Long Static State Lives: The Transaction
Assume basic Apex syntax, collections, objects, methods, and Salesforce transactions. A static variable belongs to a class rather than to an object. Salesforce keeps its value while the current transaction runs. A new transaction starts with a fresh static state.
public class TriggerGuard {
private static Boolean hasRun = false;
public static Boolean shouldRun() {
if (hasRun) {
return false;
}
hasRun = true;
return true;
}
}A trigger handler can call TriggerGuard.shouldRun() before its work. The first call returns true; later calls in the same transaction return false. The value resets when the transaction ends. Static state is therefore useful for recursion guards and transaction-local caches, but not for permanent storage.
Static Versus Instance: What Can Access What
An instance member belongs to one object. Each object has its own copy of instance fields. A static member is shared through the class name.
| Member | Accesses | Typical use |
|---|---|---|
| Instance method | Instance fields and static members | Object behavior |
| Static method | Static members only | Utility or shared transaction logic |
| Instance field | One object | Object-specific state |
| Static field | The class | Shared transaction state |
A static method cannot use this or directly read an instance field because no particular object is implied. An instance method can use its object’s fields and can also read static members.
Constants with `final` and `static final`
final prevents a field from being assigned again after initialization. Use static final for one shared constant.
public class Limits {
public static final Integer MAX_ATTEMPTS = 3;
}A final instance field can hold a different value in each object. A static final field has one class-level value for the transaction.
Static and Instance Initialization Blocks
A static initialization block runs once when Apex first uses the class. It is useful when a static value needs several statements. Apex does not provide Java-style instance initialization blocks for per-object setup. Put that setup in a constructor.
public class Config {
public static final Map<String, Integer> LIMITS;
static {
LIMITS = new Map<String, Integer>{
'small' => 10,
'large' => 1000
};
}
}Defining and Implementing Interfaces
An interface defines required behavior without choosing the implementation. A class uses implements and supplies each method.
public interface Discount {
Decimal apply(Decimal amount);
}
public class FlatDiscount implements Discount {
private Decimal off;
public FlatDiscount(Decimal off) {
this.off = off;
}
public Decimal apply(Decimal amount) {
return amount - off;
}
}
Discount discount = new FlatDiscount(5);
System.debug(discount.apply(100)); // 95The variable has type Discount, so calling code depends on the contract. Another class can implement the same interface with a different discount rule.
Exercise
Why does a static recursion guard reset during the next transaction?
Show answer
Static fields belong to the current Apex transaction context, so Salesforce creates fresh static state when a new transaction begins.