Java Anonymous Classes
Sometimes in Java, you need a one-time class implementation—for example, creating a button click handler or a comparator for sorting. Creating a full class just for that can be unnecessary.
This is where anonymous classes come in. An anonymous class is a class without a name, defined and instantiated in a single expression.
In this guide, we’ll cover:
- What anonymous classes are
- Syntax and examples
- Common use cases
- Difference between anonymous classes and lambdas
- Best practices
What Is an Anonymous Class?
- An unnamed class declared and instantiated in one step.
- Used to extend a class or implement an interface.
- Provides a quick way to override methods without creating a separate class file.
General Syntax:
ParentClassOrInterface obj = new ParentClassOrInterface() {
// overridden methods
};
Example 1: Anonymous Class Implementing an Interface
interface Greeting {
void sayHello();
}
public class AnonymousExample {
public static void main(String[] args) {
Greeting g = new Greeting() {
@Override
public void sayHello() {
System.out.println(“Hello from Anonymous Class!”);
}
};
g.sayHello();
}
}
Output:
Hello from Anonymous Class!
Example 2: Anonymous Class Extending a Class
class Animal {
public void sound() {
System.out.println(“Animal makes a sound”);
}
}
public class TestAnimal {
public static void main(String[] args) {
Animal dog = new Animal() {
@Override
public void sound() {
System.out.println(“Dog barks”);
}
};
dog.sound();
}
}
Output:
Dog barks
Example 3: Anonymous Class with Comparator
Anonymous classes are often used for sorting with custom logic.
import java.util.*;
public class ComparatorExample {
public static void main(String[] args) {
List<String> names = Arrays.asList(“Charlie”, “Alice”, “Bob”);
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return b.compareTo(a); // reverse order
}
});
System.out.println(names);
}
}
Output:
[Charlie, Bob, Alice]
Anonymous Classes vs Lambdas
Since Java 8, lambdas are often used instead of anonymous classes, especially for functional interfaces.
Example with Lambda:
Collections.sort(names, (a, b) -> b.compareTo(a));
- Use anonymous classes for non-functional interfaces or when extending classes.
- Use lambdas for functional interfaces (interfaces with a single abstract method).
When to Use Anonymous Classes
- For quick, one-time use implementations.
- For event listeners (like button clicks in GUI).
- For comparators and other short-lived objects.
- When extending a class for temporary behavior.
Best Practices
- Use anonymous classes only for short, simple logic.
- Prefer lambdas for functional interfaces.
- Avoid writing large, complex anonymous classes (hard to read/maintain).
- Use them when creating a separate class is unnecessary.
Conclusion
Java anonymous classes are a powerful way to create temporary, one-off implementations without the overhead of defining a new class.
- They’re ideal for quick event handlers, comparators, and short-lived customizations.
- With Java 8+, lambdas are often preferred for functional interfaces.
By mastering anonymous classes, you’ll make your Java code more concise, flexible, and expressive.