O'Reilly logo

Java Garage by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Anonymous Inner Classes

Method local inner classes define a class inside the body of a method. But you can also define a class as a method parameter. The anonymous inner class is called anonymous because it is never given a name.

The key distinguishing characteristic of anonymous inner classes is that they are declared and instantiated at the same time.

The most common place to see anonymous inner classes is in GUI event handlers. The idea is to keep the code that handles an event in the same place where the control is defined. You create them when you need a class that you only need to call from one place, when one specific thing happens. Because they're defined as part of a method, they must adhere to the same restrictions that method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required