3

the guide says that one difference between Java and Apex is:

Inner classes and interfaces can only be declared one level deep inside an outer class.

Considering I know in Java what is an Inner class, an Interface and an Anonymous class, could you bring an example so to understand what "one level deep inside" means? My guess is that could be something regarding OOP and the structure of inherithance a class or is considering the level deep as a nested structure, so that an inner classes

EDIT: there is an idea of Lambdas, please feel free to vote at this official link, if you are beginner, see how lambdas make the code more readable the code with this simple example

1 Answer 1

8

It means that an inner class can't have an inner class in Apex. So a structure like this is legal and common, especially when implementing patterns like wrapper classes or dependency injection:

public with sharing class OuterClass {
    private class InnerClass {
        // Members here.
    }

    // Members here
}

However, this structure is not legal Apex:

public with sharing class OuterClass {
    private class InnerClass {
        private class InnerInnerClass {
            // Members here.
        }

        // Members here.
    }

    // Members here
}

With the same prohibition applying for inner interfaces as inner classes.

Note that this stricture is written in terms of inner declaration. It's about the class nesting hierarchy, not the inheritance tree.

5
  • accepted answer, so I am afraid also anonymous inner classes and lambda expressions are not supported because of the nesting hierarchy of the class. This makes the code more difficult to read, but I guess the reasons are in the efficiency that wants to be obtained at runtime, because nested classes are going to be more expensive I reckon, and overall salesforce wants to maintain the code easier as possible because of the cloud structure. Could be correct? Commented Apr 15, 2018 at 14:10
  • You are correct. Apex supports neither anonymous classes nor lambda expressions (nor closures, nor function application).
    – David Reed
    Commented Apr 15, 2018 at 14:11
  • you got my upvote as well Commented Apr 15, 2018 at 14:18
  • 3
    @trocchietto Like most features, it's really just a question of prioritization. There's an idea for lambdas if you want to upvote it. You could also check for existing ideas for other features and suggest them if they aren't posted already. Commented Apr 15, 2018 at 14:29
  • @IllusiveBrian Lambdas are really important to me, added EDIT on my post so to inform in the future Commented Apr 15, 2018 at 16:08

You must log in to answer this question.

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