Re: JDK implementation of inner classes doesn't match Java Language Specification
"Andreas Leitgeb" <avl@gamma.logic.tuwien.ac.at> wrote in message
news:slrng9vutb.4jb.avl@gamma.logic.tuwien.ac.at...
Lew <noone@lewscanon.com> wrote:
Mitigating this concern, I can think of no use case for
non-constant static
members in an inner class that couldn't be served at least as well
by a static
member in a more outer context.
While a static member in the outer class may have just the correct
"life-time", it may still have a wrong "scope". Encapsulation is
the
keyword. Why should I place a variable in a wider scope than where
it is really used?
That said, I can't think of an practical example, where I'd
access some static variable only inside some inner class.
Static methods are also forbidden, and sometimes they're useful. As
you point out, the only workaround is to declare them with the wrong
scope. (And if you need mutexes to use in your static methods, those
would be static fields.)
PS: Does anyone know, when inner classes (e.g. one embedded in a
method)
is actually *loaded*? Together with the outer class, or only when
the enclosing method is invoked? Since the inner class has no
static-
initializers, it's hard to tell from experiment. Hmm, perhaps
removing
the inner .class file and watching for ClassNotFound errors may
tell.
As far as I know, inner class is a compile-time-only concept: to the
JVM, they're no different from any other class, and won't be loaded
until needed.