Sub topic 5
Static variables and methods: state per class
This chapter assumes you can write basic Apex (variables, control flow, collections) and that you know how to declare a class with fields, methods, and constructors. Instance members hold state for each object. Static members hold state for the class itself.
What static means
A member marked static belongs to the class, not to any one object. Apex keeps one copy of a static variable no matter how many objects you create. You reach it through the class name, such as Counter.instancesCreated.
An instance variable works differently. Every object gets its own copy.
A worked example
The Counter class below mixes both kinds of member.
public class Counter {
public static Integer instancesCreated = 0;
public static final Integer MAX_VALUE = 100;
private Integer count;
public Counter() {
this(0);
}
public Counter(Integer startAt) {
this.count = startAt;
instancesCreated++;
}
public void increment() {
if (count < MAX_VALUE) {
count++;
}
}
public Integer getCount() {
return count;
}
public static Counter createStartingAt(Integer value) {
return new Counter(value);
}
}
// Usage
Counter a = new Counter();
a.increment();
Counter b = Counter.createStartingAt(5);
System.debug(a.getCount()); // 1
System.debug(b.getCount()); // 5
System.debug(Counter.instancesCreated); // 2Each Counter has its own count, so a and b report different values. Both constructors run instancesCreated++, and this(0) passes through the second constructor. The result is a single shared total of 2.
Static methods
A static method runs without an object. You call it as Counter.createStartingAt(5). Static methods suit three jobs:
- Factory methods that build and return objects, as above.
- Utility functions that depend only on their parameters.
- Code that reads or updates shared static state.
Apex does not let a static method read instance fields or use this. No object exists when the method runs, so there is no count to read. An instance method can use static members freely. The next chapters cover these rules in full.
Common mistakes
- Calling through an instance. Write
Counter.instancesCreated, nota.instancesCreated. Apex rejects static access through an object reference. - Treating a static as permanent storage. The value is not saved anywhere. It is shared only within the running code, and the lifetime chapter explains exactly how long it lasts.
- Hiding mutable state. A public static variable can be changed from anywhere. If outside code should only read it, make it
privateand expose a static getter method.
Static versus instance at a glance
| Feature | Instance member | Static member |
|---|---|---|
| Copies | One per object | One per class |
| Accessed with | An object reference | The class name |
| Can use `this` | Yes | No |
| Needs `new` first | Yes | No |
Exercise
In the Counter class, you create three objects with new Counter(). What is Counter.instancesCreated afterward, and what does each object's count hold? Why do these differ in how they are stored?
Show answer
instancesCreated is 3, because it is a static variable with one copy shared by all objects, and each constructor call increments it. Each object holds its own count, which is 0 unless you passed a different start value, because count is an instance variable and every object has its own copy.