CKTIS / Java
Anonymous classes in Java
An anonymous class lets you define an interface implementation or a subclass and create an instance of it in the same expression.
Version: Java SE 21
Syntax
Anonymous implementation: new InterfaceType() { /* member declarations */ }
Anonymous subclass: new BaseType(constructorArguments) { /* member declarations */ }
In a variable declaration, end the declaration with a semicolon after the closing brace.In Java SE 21, new Interface() { ... } declares an anonymous class and creates an instance of it. Interface stands for the name of an existing interface, and ... stands for declarations in the class body. The object belongs to the implementing class, not to the interface itself. Each evaluation creates a fresh object; each separate occurrence of this form declares a distinct class.
The form new BaseType(args) { ... } creates a subclass instance, provided BaseType can be extended and a suitable constructor can be called. The arguments go to the superclass constructor. You cannot explicitly declare a constructor for the anonymous class, but its body can contain fields, methods, and instance initializers.
The class must supply implementations for all required abstract methods. Implementations of Action.perform(), Runnable.run(), and Executor.execute(Runnable) must be public. Use @Override so the compiler can catch a signature mistake, such as accidentally overloading a method instead of overriding it.
In an instance method of an anonymous class, this refers to that object. A lambda introduces no this of its own and needs a functional interface as its target type. Both forms can capture local variables only if they are final or effectively final, like label, which is never reassigned.
new Executor() { public void execute(Runnable r) ... } sketches an anonymous implementation of Executor, not the creation of a thread pool. Here execute calls r.run() directly, so the task runs synchronously on the calling thread. Other Executor implementations may use a different thread or a pool.
Examples
import java.util.concurrent.Executor;
public class AnonymousClassDemo {
interface Action {
void perform();
}
public static void main(String[] args) {
Action open = new Action() {
@Override
public void perform() { System.out.println("Open"); }
};
Action close = new Action() {
@Override
public void perform() { System.out.println("Close"); }
};
Executor direct = new Executor() {
@Override
public void execute(Runnable r) {
r.run();
}
};
String label = "Task ";
Runnable[] tasks = {
new Runnable() {
@Override
public void run() { System.out.println(label + "1"); }
},
new Runnable() {
@Override
public void run() { System.out.println(label + "2"); }
},
new Runnable() {
@Override
public void run() { System.out.println(label + "3"); }
},
new Runnable() {
@Override
public void run() { System.out.println(label + "4"); }
}
};
open.perform();
for (Runnable task : tasks) {
direct.execute(task);
}
close.perform();
}
}Result
Open Task 1 Task 2 Task 3 Task 4 Close
Two distinct anonymous classes implement `Action`, and four more implement `Runnable`. `Open` comes before `Task 1` through `Task 4`, and `Close` comes after all four tasks because each `execute` call waits for its direct call to `run()` to finish.
Related lessons
- Java · Lesson 25 · Advanced · Version: 2
- Java · Lesson 8 · Intermediate · Version: 2
- Java · Lesson 51 · Intermediate · Version: 2
Sources
- Java Language Specification, Java SE 21 — §15.9 Class Instance Creation Expressions
- Java Language Specification, Java SE 21 — §15.9.5 Anonymous Class Declarations
- Java Language Specification, Java SE 21 — §8.1.3 Inner Classes and Enclosing Instances
- Java Language Specification, Java SE 21 — §8.4.8.3 Requirements in Overriding and Hiding
- Java Language Specification, Java SE 21 — §15.27 Lambda Expressions
- Executor (Java SE 21 & JDK 21)
- Runnable (Java SE 21 & JDK 21)