JavaAnonymous Classes

Anonymous Classes

An anonymous class lets you define a class and create a single instance of it, both at once, inline in an expression — with no class name at all. It's most often used to provide a quick, one-off implementation of an interface or abstract class right where it's needed, without writing a separate named class file.

Syntax

new InterfaceName() { ... }

Java
interface Greeter {
    void greet();
}

public class Main {
    public static void main(String[] args) {
        Greeter g = new Greeter() { // defines AND instantiates in one step
            @Override
            public void greet() {
                System.out.println("Hello from an anonymous class!");
            }
        };

        g.greet();
    }
}

There's no name after new Greeter() — the body immediately following it is the class definition, and it produces exactly one instance of that unnamed class, assigned straight into g.

The classic pre-lambda use case

Before Java 8, anonymous classes were the standard way to implement a single-method interface inline — most famously for event listeners in GUI code and for Runnable, the interface behind starting a thread.

An old-style Runnable

Java
Runnable task = new Runnable() {
    @Override
    public void run() {
        System.out.println("Running in a background thread.");
    }
};

new Thread(task).start();
Lambdas have largely replaced this pattern
Note
Since Java 8, when the interface has exactly one abstract method (a **functional interface**, like `Runnable` above), a lambda expression does the same job in far less code: `Runnable task = () -> System.out.println("Running!");`. See the dedicated **Lambda Expressions** page for the full syntax — for single-method interfaces, prefer a lambda over an anonymous class today.
Where anonymous classes are still needed

Lambdas only work for interfaces with exactly one abstract method. Anonymous classes remain the right tool in two situations lambdas can't cover:

  • Implementing an interface (or extending an abstract class) that has more than one method to implement.

  • When the implementation needs its own instance state — fields that persist across multiple method calls on that same object.

An anonymous class with instance state and multiple methods

Java
interface Counter {
    void increment();
    int current();
}

public class Main {
    public static void main(String[] args) {
        Counter counter = new Counter() {
            private int count = 0; // instance state — a lambda can't hold this

            @Override
            public void increment() {
                count++;
            }

            @Override
            public int current() {
                return count;
            }
        };

        counter.increment();
        counter.increment();
        System.out.println(counter.current()); // 2
    }
}
  • An anonymous class defines and instantiates a class in one expression, with no name.

  • Classic use: inline implementations of a single-method interface, before Java 8.

  • Lambda expressions now cover that same case far more concisely.

  • Anonymous classes are still necessary for multi-method interfaces or when the implementation needs its own instance state.