Programming with Inner Classes in Java
Inner classes are an advanced programming feature in Java. They add another dimension to the concept of Object Oriented programming by allowing objects to not only reside alongside one another, but also within one another too. In this post I’ll explain how inner classes work with the help of an example that illustrates their most common use: Swing GUI programming.
Extending the Object Oriented Programming Model
Encapsulation can be regarded as a barrier that an object puts up to protect itself from other objects. Within an object we generally expect there to be fields (that each object will have it’s own instance of) and methods (that can be called to affect that object). Inner classes extend Java’s OO model by also allowing objects to be created within an object, thus making the code within an object truly object oriented.
To demonstrate this, lets start by looking at some less-than-ideal Swing GUI code. The DemoUI class is a simple application frame that uses a WindowCloser object as a listener for its window events. The code for both of these classes is shown here:
public class DemoUI extends JFrame {
public DemoUI() {
//add the window closing event listener
this.addWindowListener(new WindowCloser(this));
}
public void shutdown() {
int answer = JOptionPane.showConfirmDialog(this, "Are you sure?");
if (answer == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
class WindowCloser extends WindowAdapter {
private DemoUI demoUI;
public WindowCloser(DemoUI demoUI) {
this.demoUI = demoUI;
}
public void windowClosing(WindowEvent windowEvent) {
demoUI.shutdown();
}
}
When the user clicks application frame’s close button (i.e. the cross button on the frame’s title bar), the windowClosing method will be called on the event listener which will, in turn, call the shutdown method of the frame. The shutdown method prompts the user to make sure that they want to quit before calling System.exit(…) to exit the application.
Although a fairly simple example, this code is more complex than it need be. The reason for this complexity stems from the fact that the objects created from these classes are completely separate from one another, we could picture these objects as follows:

Because these objects would otherwise have no association with one another, we must provide a reference from the listener to the frame in order that the frame’s shutdown method may be called by the listener. It also means that the frame’s shutdown method cannot be declared private, thus weakening its encapsulation.
Applying Inner Classes
If we make the listener an inner class of the frame, then it will have access to all members of the frame (even private ones). We do this simply by moving the listener class definition inside the frame class:
public class DemoUI extends JFrame {
public DemoUI() {
//add the window closing event listener
this.addWindowListener(new WindowCloser());
}
private void shutdown() {
int answer = JOptionPane.showConfirmDialog(this, "Are you sure?");
if (answer == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent windowEvent) {
shutdown();
}
}
}
Now that the listener is declared as an inner class of the frame, instances of the listener will be created inside the enclosing frame instance. In contrast to the above example, this can now be pictured as follows:

Note that there is now no need to keep a reference to the frame instance from the listener as the listener object is implicitly associated with it. This means that the shutdown method, although declared as private on the frame, is still available to the listener. Finally, and for good measure, because the listener is only ever used by the frame, we can make its class definition private and prevent any other classes from accessing it.
Method Level Inner Classes
The example above shows an inner class being defined at the same level of scope as the methods and fields (aka the members) of the outer class. This type of inner class is therefore commonly referred to as a member level inner class.
In Java it is common to declare variables where they are first used. It is possible to do this with inner classes too. In this example the inner class has been declared just before it is used; i.e. within the constructor method of the frame:
public class DemoUI extends JFrame {
public DemoUI() {
//add the window closing event listener
class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent windowEvent) {
shutdown();
}
}
this.addWindowListener(new WindowCloser());
}
private void shutdown() {
int answer = JOptionPane.showConfirmDialog(this, "Are you sure?");
if (answer == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
A method level inner class can only be used in the method it is declared in and it is inherently private to that method. Classes defined at method level also have the additional advantage of being able to access local variables in that method, just so long as they are declared as final.
Anonymous Inner Classes
The next progression from a method level inner class is an anonymous inner class. In the previous example, the class was defined just before it was created and used. The name for the class was therefore only ever used in the definition and then in the subsequent call to create an instance of it. In cases such as this, it is possible to create and define the inner class in a single statement, rendering its name irrelevant:
public class DemoUI extends JFrame {
public DemoUI() {
//add the window closing event listener
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
shutdown();
}
});
}
private void shutdown() {
int answer = JOptionPane.showConfirmDialog(this, "Are you sure?");
if (answer == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
You can see from this code that the inner class has now been created using the Java ‘new’ operator but that the name WindowCloser is no longer used; just the supertype WindowAdapter is used instead and the implementation of the windowClosing method is simply specified in the curly braces following the normal constructor call.
This example demonstrates how to create an anonymous subclass of the WindowAdapter class which is an abstract class defined within the Swing APIs. You can create an anonymous subclass of any class though using this syntax; just place the code for the subclass within the curly braces and you’re done.
It is also possible to implement an interface using this syntax. So for example if there were a Quit button in our GUI, then we could implement the ActionListener interface for the event listener that reuses the shutdown method like so:
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
shutdown();
}
});
Summary
The discussion I’ve presented above illustrates the most common use of inner classes but there are dozens of other uses; for example, the Spring data abstraction layer uses them to great effect, as does the Wicket web framework.
Fundamentally, inner classes in Java enable objects to be created within objects. This simple principle opens up a whole array of design choices for object aggregation. With careful consideration, inner classes allow your code to be more concise and maintainable than it might otherwise be. A good understanding of inner classes is therefore a key skill for any serious Java developer.
