If I have 2 classes, Class A and class B. Class B has an inner class called Class C. Class A calls class C. My question is whether class c enforces sharing rules or not.

Public with sharing class a{
  b newb = new b();
  b.c inner = newb.new c();
  Integer a = inner.testing();

Public without sharing class b{

  class c{
    public static Integer testing(){
      return 5;


(Correct me if I am wrong) but usually wherever the method located (i.e. which class it is implemented), it uses that classes sharing rule. So it does not take into account where the method is being called from. Since Inner classes are separate to outer classes, do you need to give the sharing keyword to the inner class? Or does it use the outclass's sharing rules?

I am a little confused with sharing rules using inner classes. Any help appreciated!

  • You might also be interested in the answers on this question, which go in to a lot more detail. – sfdcfox Sep 6 '18 at 15:43

From the documentation, two critical rules:

Inner classes do not inherit the sharing setting from their container class.

Classes inherit this setting from a parent class when one class extends or implements another.

And when no sharing model is declared or inherited,

If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that the class doesn’t enforce sharing rules except if [...] the class is called by another class that has sharing enforced.

So here we have a, with sharing, calling a method on the inner class b.c, which does not declare a sharing model. Since inner classes do not inherit sharing, and b.c has no superclass, it will acquire a with sharing context from the calling class, a.

If, rather than being an inner class, c were a subclass of b:

public virtual without sharing class b {

public class c extends b {
    public static Integer testing() {
      return 5;

here, c would in fact be a without sharing class, because it inherits that declaration from its superclass - and this would apply regardless of who called c.testing().

  • If for example class a was also not defined with the sharing keyword, does that mean it would just run in the system context? Or does the behaviour become unpredictable? Edit: say it was NOT called from chatter or execute anonymous – M guy Sep 6 '18 at 15:44
  • 2
    To add more, after winter 19 release you can declare inner class as inherited sharing to avoid confusion. releasenotes.docs.salesforce.com/en-us/winter19/release-notes/… – Ranga Sep 6 '18 at 15:44
  • 1
    @Mguy, lack of a sharing declaration at the "top" of the call sequence is equivalent to without sharing. The unpredictability you might see is when code without a sharing declaration is called by multiple classes with different sharing declarations, which will result in different results based on the caller. The inherited sharing declaration Ranga pointed to helps establish safer, more predictable defaults. – David Reed Sep 6 '18 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.