Java Reference
In-Depth Information
would lose the important information about the subsidiary nature of the
class.
Static nested classes are members of their enclosing type. Static nested
classes enclosed in an interface are implicitly public; if enclosed by a
class, you can declare them to be accessible in any way you like. You
can, for example, declare a class that is an implementation detail to be
private
. We declare
Permissions
to be
public
because programmers using
BankAccount
need to use the class.
Since
Permissions
is a member of
BankAccount
, the
Permissions
class can
access all other members of
BankAccount
, including all inherited mem-
bers. For example, if
Permissions
declared a method that took a
BankAc-
count
object as an argument, that method would be able to directly ac-
cess both the
number
and
balance
fields of that account. In this sense the
nested class is seen as part of the implementation of the enclosing class
and so is completely trusted.
There is no restriction on how a static nested class can be extendedit
can be extended by any class to which it is accessible. Of course, the ex-
tended class does not inherit the privileged access that the nested class
has to the enclosing class.
Nested enum classes are always static, although by convention the
stat-
ic
modifier is omitted from the
enum
declaration. Enum classes are de-
5.1.2. Nested Interfaces
Nested interfaces are also always static and again, by convention the
static
modifier is omitted from the interface declaration. They serve
simply as a structuring mechanism for related types. When we look at
non-static nested classes you will see that they are inherently concerned
with implementation issues. Since interfaces do not dictate implement-
ation they cannot be non-static.