Does apex have anything like anonymous inner classes?

My question is pretty much in the title. I would like to create an interface and then initialize it with a definition of the methods right then and there. Anyone familiar with Java would recognize this style of code.

If anyone wants use case, A Filter interface with a method filterOut. Another class has a method that takes a list of sobjects and an object of type Filter and returns only the items that match the conditions layed out in that Filter’s filterOut method. Rather than create a pile of named inner or outter classes, it woubl be nice to just go:

filterList(listOfObjects, new Filter{
    Boolean filterOut(SObject obj){
        return == 'Bob';

Can anyone tell me if this is possible?


Afraid it’s not supported. Apex does have inner classes, up to one level deep, but there is no support for anonymous inner classes.

The closest you’re going to be able to do is make private inner classes and use those in methods, which half defeats the point.

Source : Link , Question Author : Bob Roberts , Answer Author : ca_peterson

Leave a Comment