Java Reference
In-Depth Information
C H A P T E R 3
Exploring Advanced Language Features
Chapters 1
and
2
introduced youtoJava'sfundamental language features alongwithits
supportforclassesandobjects.
Chapter3
buildsontothisfoundationbyintroducingyou
toJava'sadvanced language features, specifically thosefeatures related tonestedtypes,
packages, static imports, exceptions, assertions, annotations, generics, and enums.
Nested Types
Classes that are declared outside of any class are known as
top-level classes
. Java also
supports
nested
classes,whichareclassesdeclaredasmembersofotherclassesorscopes.
Nested classes help you implement top-level class architecture.
There are four kinds of nested classes: static member classes, nonstatic member
classes, anonymous classes, and local classes. The latter three categories are known as
inner classes
.
Thissectionintroducesyoutostaticmemberclassesandinnerclasses.Foreachkind
ofnestedclass,Iprovideyouwithabriefintroduction,anabstractexample,andamore
practicalexample.Thesectionthenbrieflyexaminesthetopicofnestinginterfaceswith-
in classes.
Static Member Classes
A
static member class
isa
static
memberofanenclosingclass.Althoughenclosed,it
doesnothaveanenclosinginstanceofthatclass,andcannotaccesstheenclosingclass's
instance fields and invoke its instance methods. However, it can access the enclosing
class's
static
fieldsandinvokeits
static
methods,eventhosemembersthatarede-
Listing 3-1.
Declaring a static member class