11

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?

2
  • 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
    Commented Sep 4, 2015 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
    Commented Sep 4, 2015 at 18:57

1 Answer 1

2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .