In Java, an anonymous class is a class that is defined without a name. It is a class that is used for a short-lived purpose, typically when you need to create a one-time object for a particular action. Anonymous classes enable you to instantiate and define the class in a single expression, which is useful for simplifying your code when you don’t need a named class.
In this guide, we will explore the concept of anonymous classes, their uses, syntax, examples, and how they differ from regular classes.
An anonymous class is a class that is defined and instantiated at the same time, without providing a class name. It is a convenient and concise way to implement classes or interfaces when you need them only once, such as when implementing event listeners or callbacks.
Anonymous classes are typically used when:
There are mainly two types of anonymous classes in Java:
An anonymous inner class is an anonymous class that extends an existing class (usually abstract or a superclass) without explicitly naming the class. You define the class and instantiate it in one go, usually in the context of a method or constructor.
abstract class Animal {
abstract void sound();
}
public class Main {
public static void main(String[] args) {
// Anonymous inner class extending Animal
Animal dog = new Animal() {
void sound() {
System.out.println("Woof! Woof!");
}
};
dog.sound(); // Output: Woof! Woof!
}
}
In this example:
Animal
abstract class and overrides the sound()
method.An anonymous class can also be used to implement an interface without explicitly defining a class for it. This is commonly seen in event handling.
interface Greeting {
void greet();
}
public class Main {
public static void main(String[] args) {
// Anonymous class implementing the Greeting interface
Greeting greetMorning = new Greeting() {
public void greet() {
System.out.println("Good Morning!");
}
};
greetMorning.greet(); // Output: Good Morning!
}
}
Here:
Greeting
interface and provides an implementation for the greet()
method.The syntax for an anonymous class in Java is:
ClassName referenceVariable = new ClassName() {
// Override methods or implement abstract methods
};
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Anonymous class in action!");
}
};
new Thread(r).start();
In this case:
Runnable
interface and overrides the run()
method.Thread
and execute the task.Here are some common use cases for anonymous classes in Java.
interface Calculator {
int add(int a, int b);
}
public class Main {
public static void main(String[] args) {
// Anonymous class implementing the Calculator interface
Calculator calculator = new Calculator() {
@Override
public int add(int a, int b) {
return a + b;
}
};
System.out.println("Sum: " + calculator.add(5, 10)); // Output: Sum: 15
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Anonymous Class Example");
JButton button = new JButton("Click me");
// Anonymous class for action listener
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
In this example:
ActionListener
interface for handling button click events.Runnable
objects or extending the Thread
class in multi-threaded applications.