Re: JDK implementation of inner classes doesn't match Java Language
Specification
olafmat1@gmail.com wrote:
According to the Java Language Specification 3.0: "A nested class is
any class whose declaration occurs within the body of another class or
interface." (top of the "8. Classes" chapter), and (?8.1.3) "An inner
class is a nested class that is not explicitly or implicitly declared
static. Inner classes may not declare static initializers (?8.7) or
member interfaces. Inner classes may not declare static members,
unless they are compile-time constant fields (?15.28)."
Repeating from ?8.1.3:
"An inner class is a nested class that is not *explicitly or implicitly*
declared static." [emphasis added]
And now, from ?9.5:
"Interfaces may contain member type declarations (?8.5). A member type
declaration in an interface is *implicitly static* and public."
[emphasis added]
class A {
interface B {
class C { //It's an inner class, isn't it?
No. It's a nested class, since it's static. The interface is also
static, if you didn't catch that as well.
In the following code, class C has been finally recognized as an inner
class:
interface A {
class B { //Class B directly encloses C
class C {
static int v; //No error???
test.java:4: inner classes cannot have static declarations
static int v; //No error???
^
Tested on javac version 1.6.0_10-rc
jcranmer@quetzalcoatl ~ $ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Server VM (build 10.0-b23, mixed mode)
Is it a bug, or did I miss something?
Most likely a bug.
BTW, I don't understand why use of static members is limited at all.
Imagine this:
class Outer {
class Inner {
public static int value;
}
}
How "static" is value? Is it static per "instance" of Inner (implicitly,
every Inner object has a reference to its parent Outer object), or is it
static per "instance" of Outer? I can think of use cases for either
manner of definition and it's not clear which one should be preferred.
So static stuff (excluding compile-time constants) is forbidden and
everyone's all happy.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth