Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Object-Oriented Concepts

Anonymous classes in Java

Anonymous classes in Java: declare and instantiate a class in one expression, capture variables, add state, and know when a lambda is the better tool.

An anonymous class is a class without a name, declared and instantiated in a single expression. You write new followed by an interface or class name and a class body. The result is an instance of a new, unnamed subclass (or implementation) of that type. It is handy for a quick one-off implementation that is used in exactly one place.

Since Java 8, lambdas replace most anonymous classes that implement a functional interface. Anonymous classes remain necessary when you need several methods, instance fields, an abstract class, or a distinct this.

Java
Runnable r = new Runnable() {
    @Override
    public void run() { System.out.println("run"); }
};

Try it

Java Anonymous implementation with state

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Subclassing a class and capturing variables

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
new InterfaceName() { /* members */ }
new ClassName(args) { /* members */ }
Aspect Rule
Supertype Exactly one: either an interface (the class extends Object) or a class; you cannot do both.
Constructor None can be declared. Arguments in the parentheses go to the superclass constructor. Use an instance initializer block { ... } for setup.
Captured locals Must be final or effectively final.
Name The compiler generates Outer$1, Outer$2, and so on; getSimpleName() returns an empty string.

Usage notes

  • this means the anonymous object, while a lambda's this is the enclosing object. Use Outer.this to reach the enclosing instance from an anonymous class in an instance method.
  • Prefer a lambda for a single abstract method that needs no state. It is shorter and does not create an extra class file.
  • Extra members are invisible from outside. With var x = new Object() { int n = 3; }; you can read x.n, but if you assign the object to a variable of the supertype, only the supertype's members are visible.
  • Hidden reference to the outer object: in an instance context the anonymous object keeps the enclosing instance alive, the same leak risk as inner classes. In a static method there is no such reference.
  • Cannot be reused or extended: each anonymous class is a single-use type; if you need it twice, give it a name.
  • Serialization and debugging: anonymous class names are unstable, so avoid serializing them.
  • Double-brace initialization (new ArrayList<>() {{ add(1); }}) creates an anonymous subclass per use and captures the outer object; avoid it. Use List.of or builders.