Java Reference
In-Depth Information
4.8.1 nested classes
Generally speaking, when we write a class, we expect, or at least hope, for it
to be useful in many contexts, not just the particular application that is being
worked on.
An annoying feature of the function object pattern, especially in Java, is the
fact that because it is used so often, it results in the creation of numerous small
classes, that each contain one method, that are used perhaps only once in a pro-
gram, and that have limited applicability outside of the current application.
This is annoying for at least two reasons. First, we might have dozens of
function object classes. If they are public, by rule they are scattered in sepa-
rate files. If they are package visible, they might all be in the same file, but we
still have to scroll up and down to find their definitions, which is likely to be
far removed from the one or perhaps two places in the entire program where
they are instantiated as function objects. It would be preferable if each func-
tion object class could be declared as close as possible to its instantiation.
Second, once a name is used, it cannot be reused in the package without pos-
sibilities of name collisions. Although packages solve some namespace prob-
lems, they do not solve them all, especially when the same class name is used
twice in the default package.
With a nested class, we can solve some of these problems. A nested class is
a class declaration that is placed inside another class declaration—the outer
class—using the keyword static . A nested class is considered a member of the
outer class. As a result, it can be public, private, package visible, or protected,
and depending on the visibility, may or may not be accessible by methods that
are not part of the outer class. Typically, it is private and thus inaccessible from
outside the outer class. Also, because a nested class is a member of the outer
class, its methods can access private static members of the outer class, and can
access private instance members when given a reference to an outer object.
Figure 4.42 illustrates the use of a nested class in conjunction with the
function object pattern. The static in front of the nested class declaration of
OrderRectByWidth is essential; without it, we have an inner class, which
behaves differently and is discussed later in this text (in Chapter 15).
Occasionally, a nested class is public. In Figure 4.42, if OrderRectByWidth
was declared public, the class CompareTestInner1.OrderRectByWidth could be
used from outside of the CompareTestInner1 class.
A nested class is a
class declaration
that is placed inside
another class dec-
laration—the outer
class—using the
keyword static .
A nested class is a
part of the outer
class and can be
declared with a visi-
bility specifier. All
outer class mem-
bers are visible to
the nested class's
methods.
4.8.2 local classes
In addition to allowing class declarations inside of classes, Java also allows
class declarations inside of methods. These classes are called local classes .
This is illustrated in Figure 4.43.
 
Search WWH ::




Custom Search