Learn Java
import com.learnjava.oopadvanced.AnonymousClasses;

Anonymous Classes in Java

@Concept

An anonymous class defines and instantiates a one-off subclass or interface implementation in a single expression, with no separate named class declaration at all — new Runnable() { public void run() { ... } } creates an object implementing Runnable right on the spot, supplying its method body inline. It exists specifically for cases where a class is only ever going to be used once, right there, and giving it a real name elsewhere in the codebase would add a small file or declaration with no real reuse value.

Since Java 8, a lambda expression can replace an anonymous class specifically when implementing a functional interface — one with exactly one abstract method — which covers the large majority of anonymous classes seen in older code. Anonymous classes are still needed for interfaces with more than one abstract method, or for extending an actual class rather than implementing an interface.

@HowItWorks
1

The syntax

new InterfaceOrClass() { ... } both defines and instantiates a one-off implementation in a single expression.

2

No separate declaration needed

The anonymous class exists only at that point in the code, with no name to reference elsewhere.

3

Implementing an interface anonymously

The braces supply a full implementation of the interface's method or methods, right inline.

4

Extending a class anonymously

The same syntax can also create a one-off subclass of a real class, overriding whichever methods are needed.

5

Lambdas replace many anonymous classes

But only for functional interfaces with exactly one abstract method; multi-method interfaces and class extension still need the full anonymous-class syntax.

@Example
public class AnonymousDemo {
interface Greeting {
void sayHello();
}
 
public static void main(String[] args) {
Greeting g = new Greeting() {
public void sayHello() {
System.out.println("Hello from an anonymous class");
}
};
g.sayHello();
}
}
Output
Hello from an anonymous class
1new Greeting() { ... } both defines an unnamed implementation of Greeting and creates one instance of it, in a single expression.
2The body inside the braces supplies sayHello()'s complete implementation, right there inline.
3g holds a reference to that one-off object, typed as the Greeting interface.
4g.sayHello() calls it, printing the single line defined inside the anonymous class's body.
@UseCases
Providing a one-off implementation of an interface used exactly once, without a separate named class
Overriding a specific method of a real class for one particular object, without a whole new subclass file
Reading older Java code that predates lambdas, which relied on anonymous classes for this same purpose
Recognizing when a multi-method interface still requires an anonymous class instead of a lambda

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.