According to the documentation, static variables are allowed only in outer classes. Why is that? Is it because you'd need to instantiate the inner class first?

  • 1
    You can read about this subject for Java. Though as inner classes are not given automatic access to the outer class fields in Apex not all of that is relevant. – Keith C Sep 4 '15 at 15:31
  • 2
    The difference is that inner classes in java can be constructed off an instance of the outer class. MyClass outer = new MyClass(); MyInnerClass inner = outer.new MyInnerClass(); In apex, they are always treated as static on the outer class. MyClass.MyInnerClass inner = new MyClass.MyInnerClass(); Thus, it seems that statics would be perfectly fine in an inner class. I have no good answer why SF wouldn't allow them – NSjonas Sep 4 '15 at 18:57

InnerClass cannot have static members because it belongs to an instance (of OuterClass) and a static members means that this variable belongs to the entire class and not any particular instance.

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.