Java Reference
In-Depth Information
a class as an inner class if it is used only by its outer class. An inner class has the following
features:
An inner class is compiled into a class named
OuterClassName$
InnerClassName
.
class
.
For example, the inner class
A
in
Test
is compiled into
Test$A
.class
in
Figure 15.7b
.
■
An inner class can reference the data and the methods defined in the outer class in
which it nests, so you need not pass the reference of an object of the outer class to
the constructor of the inner class. For this reason, inner classes can make programs
simple and concise. For example,
circlePane
is defined in
ControlCircle
in
Listing 15.3 (line 15). It can be referenced in the inner class
EnlargeHandler
in
line 46.
■
An inner class can be defined with a visibility modifier subject to the same visibility
rules applied to a member of the class.
■
An inner class can be defined as
static
. A
static
inner class can be accessed
using the outer class name. A
static
inner class cannot access nonstatic members
of the outer class.
■
Objects of an inner class are often created in the outer class. But you can also create
an object of an inner class from another class. If the inner class is nonstatic, you must
first create an instance of the outer class, then use the following syntax to create an
object for the inner class:
■
OuterClass.InnerClass innerObject = outerObject.
new
InnerClass();
If the inner class is static, use the following syntax to create an object for it:
■
OuterClass.InnerClass innerObject =
new
OuterClass.InnerClass();
A simple use of inner classes is to combine dependent classes into a primary class. This
reduces the number of source files. It also makes class files easy to organize since they are all
named with the primary class as the prefix. For example, rather than creating the two source
files
Test.java
and
A.java
as shown in Figure 15.7a, you can merge class
A
into class
Test
and create just one source file,
Test.java
as shown in Figure 15.7b. The resulting class files
are
Test.class
and
Test$A.class
.
Another practical use of inner classes is to avoid class-naming conflicts. Two versions of
CirclePane
are defined in Listings 15.2 and 15.3. You can define them as inner classes to
avoid a conflict.
A handler class is designed specifically to create a handler object for a GUI component
(e.g., a button). The handler class will not be shared by other applications and therefore is
appropriate to be defined inside the main class as an inner class.
15.7
✓
✓
Can an inner class be used in a class other than the class in which it nests?
Check
15.8
Point
Can the modifiers
public
,
protected
,
private
, and
static
be used for inner
classes?
An anonymous inner class is an inner class without a name. It combines defining an
inner class and creating an instance of the class into one step.
Key
Point
Inner-class handlers can be shortened using
anonymous inner classes
. The inner class in
Listing 15.3 can be replaced by an anonymous inner class as shown below.
anonymous inner class
Search WWH ::
Custom Search