Inner Classes

The classes you have worked with thus far are all members of a package, either because you specified a package name with the package declaration or because the default package was used. Classes that belong to a package are known as top-level classes. When Java was introduced, they were the only classes supported by the language.

Beginning with Java 1.1, you could define a class inside a class, as if it were a method or a variable. These types of classes are called inner classes. Listing 15.5 contains the Inner applet, which uses an inner class called BlueButton to represent clickable buttons that have a default background color of blue.

Listing 15.5 The Full Text of Inner.java
 1: import java.awt.*; 2: import javax.swing.*; 3: 4: ...

Get Sams Teach Yourself Java 2 in 21 Days, 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.