1

In Apex, how many inner classes can an outer class have?

closed as off-topic by Sebastian Kessel, Raul, salesforce-sas, glls, Vijay Ganji Aug 19 at 5:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Sebastian Kessel, Raul, salesforce-sas, glls, Vijay Ganji
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Welcome to SFSE! Please take the tour and read How to Ask. I don't think there's any practical limit here, but I doubt there's documentation to back that up. This question, though, gives me an X-Y Problem vibe. I get the feeling that you either have a class with a bunch of inner classes and are running into an issue, or are trying to solve a problem and think that using a bunch of inner classes is the answer. Either way, the best way to get help from this community is to provide us with lots of details. What is your problem? How are you trying to solve it? – Derek F Aug 17 at 23:05
  • If you're getting an error message, providing the full error message, verbatim, really helps. The more relevant detail you can give us, the better we'll be able to understand your situation. Ultimately, that should end up getting you more useful and accurate answers. – Derek F Aug 17 at 23:09
  • 2
    And, for what it's worth, I've personally had somewhere between 12 - 36 inner classes contained in a single outer class (mostly just to help deserialize payloads from an external system). Haven't really counted the exact number. – Derek F Aug 17 at 23:14
9

In the name of science, I attempted to save a class with programmatically generated inner classes that looked like the following:

public with sharing class Demo
{
    public class Inner0 { }
    public class Inner1 { }
    public class Inner2 { }
    public class Inner3 { }
    // ...
}

I kept expanding this pattern until I ran into an error. Adding 1000 at a time, it worked until I hit InnerClass7000, at which point I hit a generic compile fail:

An unexpected error occurred

I walked it back until I found the exact maximum number of classes where I would not hit this error, and it is 6663. I was kind of hoping it would be 6666, but alas.

Compiles:

public with sharing class Demo
{
    // ...
    public class Inner6661 { }
    public class Inner6662 { }
    public class Inner6663 { }
}

Does not compile:

public with sharing class Demo
{
    // ...
    public class Inner6662 { }
    public class Inner6663 { }
    public class Inner6664 { }
}

If you are inquiring about how deeply nested inner classes can be, it is capped at one level.

public class Outer
{
    public class Inner
    {
        public class TooDeep
        {
            // not allowed
        }
    }
}

See also, Apex Class Definition (emphasis mine):

In Apex, you can define top-level classes (also called outer classes) as well as inner classes, that is, a class defined within another class. You can only have inner classes one level deep.

  • The unexpected error was interesting. I was guessing you could go much higher... but alas. – sfdcfox Aug 18 at 3:33
  • Yeah if I had to guess, I too would have expected a higher number. – Adrian Larson Aug 18 at 3:50
1

The language spec doesn't say, so in theory, you could have 111,535 inner classes before you got a "class too large" error for exceeding 1,000,000 characters. It appears that a compiler error limits you to an actual value of 6,663. In the "real world", having more than 5-10 classes should be a sign that you should probably refactor and/or extract some classes in other other classes or top-level classes. The exception to this rule of thumb would be automatically generated code, such as WSDL2Apex, or JSON2Apex.

  • Thanks all Appreciate it – Faiz Aug 21 at 21:26

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