11.4. Handling Events with Anonymous Inner Classes

In the previous example we defined a class within another class. Although the inner class definition appeared in an unusual location (inside another class), it still followed the same basic syntax rules as normal classes. In contrast, anonymous inner classes let you define and create a class within an expression. You never even name the class being defined. To define anonymous classes, you make what looks like a call to the constructor of the parent class, but you include the definition of the subclass right after the parentheses, as in the following two examples:

Color someColor = pickColor();
add(new Panel() {
						public Color origColor = someColor;
						public void init() {
						setBackground(origColor); ...

Get Core Web Programming, Second Edition 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.