What is the difference between static nested, inner, local and anonymous classes?
Short answer
A static nested class is not tied to an outer object; an inner class belongs to one object and implicitly holds Outer.this; an anonymous class is a nameless class written in place.
3 points to say:
- Default to static nested, like
Builder. - An inner class that outlives its outer leaks memory.
- One-off callbacks are usually lambdas now.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.