I've read in the Apex Workbook and the Apex Developer Guide the mention of inner classes. What is their purpose?
I see in the Apex Workbook that they used private variables and setter/getter methods in the inner class, but why didn't they just do that in the outer class with a constructer, getter, and setter?
Best Answer
I think this question is way too broad for one answer to cover. I suggest you look at some libraries out there to see how they use inner classes.
There are so many use cases. It would be really difficult to cover them all. But I will just cover three here:
Generalization (DRY)
I know for my
DML
library, I use a few, all of which have their behavior defined by interfaces. But that is neither here nor there.Maybe a concrete example will help you better understand. Take a look at the
GenericResult
inner class in myDMLResults
class.This class helps me out with DRY programming by letting me use one class to represent all of the
*Result
system classes (see also: Do the *Result objects in Database have a common ancestor?). Because of this implementation, myPartialResult
logic can useGenericResult
instances once instead of repeating the logic once each forSaveResult
,UpsertResult
,DeleteResult
, etc.Selection Wrappers
Often wrapper classes are often used for record selection on a
Visualforce Page
. This can be as simple as:Then in your markup, you would see something like:
Deserialization
Deserialization is much simpler using an inner class, though you can use top-level classes as well.
Then you can deserialize using this class like: