Sub topic 14
Inner classes and their restrictions
This chapter assumes you can write a top-level Apex class with fields, methods, and constructors, and that you know the difference between instance and static members. It covers classes declared inside another class, and the rules that limit them.
What an inner class is
An inner class is a class declared inside the body of another class, called the outer class. Inner classes are useful for small helper types that only make sense next to the outer class. Typical uses are wrapper objects that bundle a record with extra data, and small result holders.
public class OrderSummary {
public class Line {
public String name;
public Decimal total;
public Line(String name, Decimal total) {
this.name = name;
this.total = total;
}
}
public static List<Line> build(Map<String, Decimal> totals) {
List<Line> lines = new List<Line>();
for (String key : totals.keySet()) {
lines.add(new Line(key, totals.get(key)));
}
return lines;
}
}
// Usage
Map<String, Decimal> totals = new Map<String, Decimal>{ 'Widgets' => 40, 'Gadgets' => 60 };
List<OrderSummary.Line> lines = OrderSummary.build(totals);
System.debug(lines.size()); // 2
System.debug(lines[0].total); // 40 or 60, depending on map orderOutside the outer class, you refer to the inner class with the outer name as a prefix: OrderSummary.Line. Inside OrderSummary, plain Line is enough.
Visibility
Access modifiers work as they do elsewhere. An inner class with no modifier is private to the outer class. Declare it public or global to use it from other classes, as Line is above. Inner classes can have their own constructors, instance fields, and instance methods, and they can implement interfaces or extend virtual and abstract classes. You can also declare an interface or an enum inside a class.
Restrictions
| Restriction | What it means |
|---|---|
| One level only | An inner class cannot contain another inner class. |
| No static members | Static variables and static methods can be declared only in a top-level class. |
| No outer instance access | An inner class has no reference to an outer object, so it cannot read the outer class's instance fields. |
| Name clash | An inner class cannot have the same name as its outer class. |
The third rule differs from Java's non-static inner classes. Apex inner classes behave like Java's static nested classes, even though you never write static. If an inner class needs outer data, pass it in through the constructor.
An inner class can read the outer class's static members. This is the usual way to share a constant:
public class Report {
private static final Integer MAX_ROWS = 50;
public class Page {
public Boolean isFull(Integer rows) {
return rows >= MAX_ROWS; // reads an outer static member
}
}
}When to use one
Use an inner class when the type has no meaning outside its outer class. If other classes need the type, or the nesting makes the file hard to read, make it a top-level class. The one-level limit also means a deeply nested design needs to be flattened into separate classes.
Exercise
A developer writes an inner class Row inside Export. Export has an instance field String title, and Row has a method that returns title. Does this compile? How would you fix it?
Show answer
It does not compile. An Apex inner class has no reference to an outer object, so it cannot read the outer instance field title. To fix it, give Row its own field, for example public String title;, and set it through the constructor when the outer class creates the row. Alternatively, make title static if it truly belongs to the class.