Unravel โบ Java concepts โบ OOP and types
Anonymous classes
An anonymous class creates and defines a one-off subclass or implementation right where it is used: new Runnable() { public void run() { ...
Learn these first
At a glance
Writing a custom one-time order on a napkin instead of adding a new dish to the menu.
- 1The new Type() { ..} expression both declares an unnamed class (extending the class or implementing the interface Type) and creates one instance of it.
- 2The body can override methods, add fields and use final or effectively final local variables of the surrounding method
- 3When the type is an interface with exactly one abstract method, a lambda does the same in one lineAnonymous classes remain necessary for abstract classes, interfaces with several methods, or when you need this to mean the new object.
- this inside an anonymous class is the anonymous object; inside a lambda this is the enclosing object
- Each anonymous class is a separate class file and loaded class
- The compiler generates a class named Outer$1 with a constructor that receives the captured variables and the outer instance as hidden parameters
public class Greeter { public Runnable job(String who) { return new Runnable() { private int calls; @Override public void run() { calls++; System.out.println("hello " + who + " #" + calls); } }; }}Read the full explanation (about 1 min)
An anonymous class creates and defines a one-off subclass or implementation right where it is used: new Runnable() { public void run() { ... } }. Lambdas replace most of them.
A mental model
Writing a custom one-time order on a napkin instead of adding a new dish to the menu.
How it works
The new Type() { ... } expression both declares an unnamed class (extending the class or implementing the interface Type) and creates one instance of it.
The body can override methods, add fields and use final or effectively final local variables of the surrounding method.
When the type is an interface with exactly one abstract method, a lambda does the same in one line. Anonymous classes remain necessary for abstract classes, interfaces with several methods, or when you need this to mean the new object.
Going deeper
this inside an anonymous class is the anonymous object; inside a lambda this is the enclosing object. That difference decides which to use when the callback needs to refer to itself, for example to remove a listener.
Each anonymous class is a separate class file and loaded class. A non-static context also captures this, so a long-lived anonymous object can keep its whole outer object alive.
Under the hood
The compiler generates a class named Outer$1 with a constructor that receives the captured variables and the outer instance as hidden parameters.
Common mistakes
- Capturing a loop variable that is not effectively final.
- Leaking the enclosing activity or service by registering an anonymous listener and never removing it.
- Writing a ten-line anonymous class where a named class or lambda would read better.
How Unravel recognises it
When you paste code, Unravel looks for these signs of anonymous classes. Spotting them yourself is a good habit when you read unfamiliar code:
- Language constructs:
anon-class
Part of a learning path
- Object-oriented Java in depth ยท previous: Nested and inner classes ยท next: static and final members
Related concepts
Frequently asked questions
What is Anonymous classes?
An anonymous class creates and defines a one-off subclass or implementation right where it is used: new Runnable() { public void run() { ... } }. Lambdas replace most of them.
What are the most common mistakes with Anonymous classes?
Capturing a loop variable that is not effectively final. Leaking the enclosing activity or service by registering an anonymous listener and never removing it. Writing a ten-line anonymous class where a named class or lambda would read better.
How can I see Anonymous classes in real code?
Paste your own Java into Unravel and it will point out where anonymous classes appears, explain how it works at the detail level you choose, and draw the flow of the code around it.