Inner Anonymous Classes

It's possible to go one step further from a local inner class to something called an anonymous class. An anonymous class is a refinement of inner classes, allowing you to write the definition of the class, followed by the instance allocation.

The anonymous class is explained in Table 12-4.

Table 12-4. Anonymous class

Java term

Description

Example code

Anonymous class

This is an inner class.

It is a variation on a local class.

void foo () {  JFrame jf = new JFrame();  jf.addEventHandler(     new EventAdapter() {         myOverridingMethod(){         /* some code */         } // end method    } // end anon class  );}

The class is declared and instantiated within a single expression.

Qualities ...

Get Just Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.