Java Reference
In-Depth Information
}
class Outer {
class Inner extends HasStatic {
static final int x = 3; // OK: compile-time constant
static int y = 4; // Compile-time error: an inner class
}
static class NestedButNotInner{
static int z = 5; // OK: not an inner class
}
interface NeverInner {} // Interfaces are never inner
}
A statement or expression
occurs in a static context
if and only if the innermost method,
constructor, instance initializer, static initializer, field initializer, or explicit constructor in-
vocation statement enclosing the statement or expression is a static method, a static initial-
izer, the variable initializer of a static variable, or an explicit constructor invocation state-
An inner class
C
is a
direct inner class of a class O
if
O
is the immediately lexically en-
closing class of
C
and the declaration of
C
does not occur in a static context.
A class
C
is an
inner class of class O
if it is either a direct inner class of
O
or an inner class
of an inner class of
O
.
A class
O
is the
zeroth lexically enclosing class of itself
.
A class
O
is the
n'th lexically enclosing class of a class C
if it is the immediately enclosing
class of the
n-1
'th lexically enclosing class of
C
.
An instance
i
of a direct inner class
C
of a class
O
is associated with an instance of
O
,
known as the
immediately enclosing instance
of
i
. The immediately enclosing instance of
An object
o
is the
zeroth lexically enclosing instance of itself
.
An object
o
is the
n'th lexically enclosing instance of an instance i
if it is the immediately
enclosing instance of the
n-1
'th lexically enclosing instance of
i
.
An instance of an inner class
I
whose declaration occurs in a static context has no lexically
enclosing instances. However, if
I
is immediately declared within a static method or static
initializer then
I
does have an
enclosing block
, which is the innermost block statement lex-
ically enclosing the declaration of
I
.
For every superclass
S
of
C
which is itself a direct inner class of a class
SO
, there is an
instance of
SO
associated with
i
, known as the
immediately enclosing instance of i with re-