Below is the structure am trying to build.

Its throwing Error:

Compile Error: unexpected token: 'class'

public class maincls {    
    public class test1 {
        public String teststr {get; set;}
        public test2 testDetails {get; set;}

        public class test2 {        
            public String firstName {get; set;}
            public String lastName{get; set;}

Did I miss anything here?

  • 6
    You can only have one inner class. An inner class cannot contain another inner class – Eric Apr 3 '17 at 20:31

You only have to read the first paragraph of the Apex Developer Guide documentation on Apex Class Definition to answer this question (emphasis mine):

Apex Class Definition

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. For example:

public class myOuterClass {
   // Additional myOuterClass code here
   class myInnerClass {
     // myInnerClass code here

So pull out test2 to nest it only one level deep:

public class TopLevel
    public class Inner1
        public String someProperty { get; set; }
        public Inner2 otherProperty { get; set; }
    public class Inner2
        public String firstName { get; set; }
        public String lastName { get; set; }

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.