| Contents | Prev | Next | Inner Classes Specification |
final, private, protected, or static?
private or protected within their enclosing classes.
A class which is local to a block is not a member, and so cannot be public,
private, protected, or static. It is in effect private to the block, since it
cannot be used outside its scope.
Access protection never prevents a class from using any member of another class, as long as one encloses the other, or they are enclosed by a third class.
Any class (if it has a name) can be declared final or abstract, and any
accessible non-final named class or interface can serve as a supertype. A
compiler may also change a class to be final if it can determine that it has no
subclasses, and that there is no way for subclasses to be added later. This is
possible when a private or block-local class has no subclasses in its scope.
static
static declaration modifier was designed to give programmers a way to
define class methods and class variables which pertain to a class as a whole,
rather than any particular instance. They are "top-level" entities.
The static keyword may also modify the declaration of a class C within the
body of a top-level class T. Its effect is to declare that C is also a top-level class.
Just as a class method of T has no current instance of T in its body, C also has
no current instance of T. Thus, this new usage of static is not arbitrary.
As opposed to top-level classes (whether nested or not), inner classes cannot
declare any static members at all. To create a class variable for an inner
class, the programmer must place the desired variable in an enclosing class.
It is helpful at this point to abuse the terminology somewhat, and say, loosely,
that the static keyword always marks a "top-level" construct (variable,
method, or class), which is never subject to an enclosing instance.
This shows why an inner class cannot declare a static member, because the
entire body of the inner class is in the scope of one or more enclosing instances.
While the C language allows block-local static variables, the same effect can
be obtained in Java, more regularly and maintainably, by defining the desired
long-lived variable in the scope which corresponds to the required lifetime.
Inner Classes Specification (HTML generated by dkramer on March 15, 1997)
Copyright © 1996, 1997 Sun Microsystems, Inc.
All rights reserved
Please send any comments or corrections to john.rose@eng.sun.com