Java > Object-Oriented Programming (OOP) > Nested and Inner Classes > Anonymous Inner Classes
Anonymous Inner Class Example: Implementing a Listener
This example demonstrates the use of anonymous inner classes to implement an event listener. Anonymous inner classes are useful when you need to create a class that will only be used once, often for handling events or callbacks.
Code Example: ActionListener with Anonymous Class
This code creates a simple GUI with a button. An anonymous inner class implements the ActionListener
interface. The actionPerformed
method is overridden to display a message when the button is clicked. This is a common use case for anonymous inner classes because we only need this listener implementation once, directly where the button's action is handled.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class AnonymousListenerExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Anonymous Listener Example");
JButton button = new JButton("Click Me!");
// Anonymous inner class implementing ActionListener
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Button Clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Concepts Behind the Snippet
Anonymous inner classes are classes defined without a name. They are declared and instantiated simultaneously. They are primarily used for creating single-use implementations of interfaces or abstract classes. The key benefit is reducing code clutter by avoiding the need to define a separate named class for a simple task.
Real-Life Use Case
In GUI programming, particularly with Swing or JavaFX, anonymous inner classes are extensively used for event handling. For example, responding to button clicks, menu selections, or other user interactions often involves implementing listener interfaces with very specific, localized behavior. Another use case is for creating simple callbacks within asynchronous operations.
Best Practices
final
or effectively final. From Java 8 onwards, effectively final variables are implicitly captured.
Interview Tip
Be prepared to explain the benefits and drawbacks of anonymous inner classes. Common interview questions involve contrasting them with regular inner classes, lambda expressions (in Java 8+), and when each approach is most appropriate. Also, be ready to explain the scope and limitations of variable access from within an anonymous inner class.
When to Use Them
Anonymous inner classes are appropriate when:
Memory Footprint
Anonymous inner classes, like all classes, consume memory. Each instance of an anonymous inner class occupies memory. While the impact is usually minimal for simple implementations, be aware of potential memory usage if you create numerous instances of complex anonymous inner classes, particularly in performance-critical applications.
Alternatives
Pros
Cons
FAQ
-
What is an anonymous inner class?
An anonymous inner class is a class that is defined without a name. It is typically used to create single-use implementations of interfaces or abstract classes. -
When should I use an anonymous inner class?
Use an anonymous inner class when you need a class implementation only once, the implementation is short and straightforward, and you want to avoid creating a separate named class for a simple task. -
What are the alternatives to anonymous inner classes?
Alternatives include named inner classes, lambda expressions (in Java 8+), and regular classes. -
Can I access variables from the enclosing scope within an anonymous inner class?
Yes, you can access variables from the enclosing scope. Prior to Java 8, these variables had to befinal
or effectively final. From Java 8 onwards, effectively final variables are implicitly captured.