Sub topic 7
Static versus instance: what can access what
This chapter assumes you can write basic Apex classes with fields, methods, and constructors. It also assumes you know that Apex code runs inside a transaction. Here you will learn which members of a class can reach which other members, and why.
The core rule
An instance member belongs to one object. A static member belongs to the class itself. That difference decides what each kind of method can see.
- An instance method runs on a specific object. It can read and change that object's fields. It can also use static members, because the class exists whenever an object does.
- A static method runs with no object. There is no
this, so it cannot use instance fields or call instance methods directly. Doing so is a compile error.
| Code inside a... | Instance field or method | Static field or method |
|---|---|---|
| Instance method | Yes | Yes |
| Static method | No, unless you create or receive an object | Yes |
A static method can still work with instance members if you give it an object, for example as a parameter.
A worked example
The class below keeps a tax rate per object and a count shared by every object.
public class PriceCalculator {
public static Integer calculationsRun = 0;
private Decimal taxRate;
public PriceCalculator(Decimal taxRate) {
this.taxRate = taxRate;
}
public Decimal withTax(Decimal amount) {
calculationsRun++;
return amount + (amount * taxRate / 100);
}
public static Decimal round2(Decimal value) {
return value.setScale(2);
}
}
// Usage
PriceCalculator calc = new PriceCalculator(8);
Decimal total = PriceCalculator.round2(calc.withTax(19.99));
System.debug(total); // 21.59
System.debug(PriceCalculator.calculationsRun); // 1withTax is an instance method. It reads the instance field taxRate and updates the static counter calculationsRun. round2 is static. It needs only its parameter, so you call it through the class name without creating an object.
If you added return value * taxRate; to round2, it would not compile. A static method has no object, so taxRate has no value to read.
Common mistakes
- Using an instance field in a static method. Pass the value in as a parameter, or make the method an instance method.
- Expecting separate objects to have separate static values. Static state exists once per class. Changing it through one object's method changes it for all of them.
- Storing per-record data in a static variable. A static field holds one value at a time. If code processes many records in one transaction, each record overwrites the last. Use a collection keyed by record, or an instance field.
Choosing between them
Make a method static when it needs no object state and works only from its inputs, like round2. Make it an instance method when it depends on state held by an object, or when you want to hide the implementation behind an interface. A static method cannot implement an interface method, and callers cannot swap it for a different implementation. Later chapters return to this trade-off.
Exercise
A class has private String label; and public static String describe() { return 'Label: ' + label; }. Why does it fail to compile, and what are two ways to fix it?
Show answer
describe is static, so it runs without an object and cannot read the instance field label. One fix is to make describe an instance method. Another is to keep it static and pass the value in, as in describe(String label).