Java Anonymous Classes
A Java anonymous class is a class without a name that is declared and created in one expression. It is useful when you need one small object that implements an interface or extends a class only once.
Anonymous classes are often seen in older Java code, event handlers, and places where a tiny custom behavior is needed immediately.
Basic Syntax
An anonymous class starts with new, followed by an interface name or class name, then a class body in braces. The expression creates an object from that unnamed class.
The general idea is: create an object, provide method bodies right away, and store the result in a variable or pass it to a method.
Implementing An Interface
The most common beginner-friendly use is creating a quick implementation of an interface. The anonymous class must implement every required interface method.
interface Greeting {
void sayHello(String name);
}
public class Main {
public static void main(String[] args) {
Greeting friendlyGreeting = new Greeting() {
@Override
public void sayHello(String name) {
System.out.println("Hello, " + name + "!");
}
};
friendlyGreeting.sayHello("Maya");
}
}
Output:
Hello, Maya!
The expression new Greeting() { ... } does not create the interface itself. Instead, it creates an unnamed class that implements Greeting, then creates one object from that class.
Notice the semicolon after the closing brace. The anonymous class is part of an assignment statement, so the statement still needs a semicolon.
Extending An Abstract Class
An anonymous class can also extend a class. This is especially useful with an abstract class when you want one object with a custom implementation of an abstract method.
abstract class Task {
private String title;
Task(String title) {
this.title = title;
}
void printTitle() {
System.out.println("Task: " + title);
}
abstract void run();
}
public class Main {
public static void main(String[] args) {
Task backupTask = new Task("Backup files") {
@Override
void run() {
System.out.println("Copying important files...");
}
};
backupTask.printTitle();
backupTask.run();
}
}
Output:
Task: Backup files
Copying important files...
Here, the anonymous class calls the Task constructor with "Backup files". It then provides the missing run() method, so Java can create a concrete object.
Accessing Local Variables
An anonymous class can read local variables from the surrounding method only if those variables are final or effectively final. Effectively final means the variable is assigned once and not changed afterward.
This rule exists because the anonymous object may live longer than the method call that created it. If the object needs a local value later, Java must know that value will not change.
Anonymous Classes Vs Named Classes
Use an anonymous class when the behavior is short, clear, and needed in only one place. Use a normal named class when the code is longer, reused, or important enough to test and describe separately.
- Anonymous classes have no reusable class name.
- They can implement an interface or extend one class.
- They can have fields and methods, but only methods available through the variable type are easy to call from outside.
- For simple one-method interfaces, modern Java often uses lambda expressions instead.
Common Mistakes
- Forgetting to implement all required methods from the interface or abstract class.
- Leaving off the semicolon after the anonymous class expression.
- Trying to change a local variable after an anonymous class reads it.
- Using an anonymous class for a large block of code that would be clearer as a named class.
Takeaway: anonymous classes let you create a one-time object with custom behavior exactly where that object is needed.
