What is the difference between a lambda expression and an anonymous inner class?

Lambda expressions and anonymous inner classes both let you supply behavior without a named top-level class, but a lambda is a compact syntax limited to implementing a single functional interface method, while an anonymous class can implement multiple methods and has its own distinct scope and type identity.

Key Points

Example

Implementing a Runnable with new Runnable() { public void run() {...} } is an anonymous class, whereas Runnable r = () -> {...} achieves the same result as a lambda with far less boilerplate — but only because Runnable happens to be a functional interface.

// Anonymous inner class
Runnable r1 = new Runnable() {
    @Override
    public void run() {
        System.out.println("Anonymous class: " + this);
    }
};

// Lambda expression
Runnable r2 = () -> System.out.println("Lambda: " + this);

Interview Tip

A concise interview answer is

Both let you supply behavior inline, but a lambda only works for a functional interface with a single abstract method, while an anonymous class can implement multiple methods or extend a class. The key gotcha is this: inside a lambda it refers to the enclosing instance, but inside an anonymous class it refers to the anonymous class instance itself — that trips people up in interviews.

Last updated: 11 November 2026

Previous question: What is a lambda expression in Java 8, and what are its benefits?

Next question: What is a functional interface in Java 8?

More Java 8 interview questions

All Java 8 questions