Sub topic 9
Inner classes and sharing
This chapter assumes you know the with sharing, without sharing, and inherited sharing keywords from the earlier chapters. It also assumes you are comfortable with basic Apex classes and SOQL.
Inner classes do not inherit the outer keyword
An inner class is a class declared inside another class. It is easy to assume that an inner class takes on the sharing mode of the class that contains it. It does not. The keyword on the outer class applies to the outer class's own methods only.
Each inner class has its own sharing declaration. You can put with sharing, without sharing, or inherited sharing directly on the inner class. If you leave the keyword off, the outer class's keyword is not applied to it. Its mode then follows the rules for an omitted keyword, so it depends on how it is called.
Example: isolating one privileged operation
A common use is to keep a class strict and put one narrow, deliberate exception in a small inner class.
public with sharing class CaseReport {
public static Integer countVisible() {
// Runs with sharing: only Cases the user can see
return [SELECT COUNT() FROM Case];
}
public static Integer countAll() {
// Delegates to the inner class, which declares its own mode
return new AllCaseCounter().count();
}
private without sharing class AllCaseCounter {
public Integer count() {
// Ignores record-level sharing, whatever the outer class says
return [SELECT COUNT() FROM Case];
}
}
}countVisible and countAll can return different numbers for the same user. The outer class is with sharing, but AllCaseCounter declares without sharing, so its query sees every Case. Making the inner class private keeps other classes from calling it.
Inner classes cannot declare static methods or static variables, so the example creates an instance with new.
Why this matters
Reading only the outer class declaration gives you the wrong picture. A class that opens with public with sharing class can still contain code that bypasses record-level sharing. A reviewer, or you six months later, has to check every inner class.
The reverse also happens. A without sharing outer class can contain an inner class marked with sharing, which is a good way to keep part of a privileged class under the user's access.
Guidelines
- Declare a sharing keyword on every inner class. Do not rely on the outer class's keyword or on the omitted-keyword behavior.
- Use
without sharingin an inner class only for a small, named operation, and give the class a name that says what it does. - Mark such inner classes
privateso the exception cannot be reached from other classes. - Use
inherited sharingon an inner class that should follow its caller, such as a helper that several callers share.
Exercise
A top-level class is declared public with sharing. It contains an inner class declared private without sharing whose method runs [SELECT COUNT() FROM Account]. When the outer class calls that method, does the query respect the user's record-level sharing? What should you add if you want an inner class to follow its caller's mode instead?
Show answer
No. The inner class declares its own mode, so the query runs without sharing and counts all Accounts, even though the outer class is with sharing. To make an inner class follow its caller, declare it inherited sharing.