SteelReed

Search

Search posts, questions…

Java CoreQuestion 19 / 43

Junior

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.

one-off callbacknamed classnoyesNeed a nested classLambda oranonymous classNeeds outer's data?Static nesteddefault, BuilderInner classholds Outer.this

3 points to say:

  1. Default to static nested, like Builder.
  2. An inner class that outlives its outer leaks memory.
  3. 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.