From: Gilad Bracha
Here's another test case I got from Mark Lillibridge, whose doing a Java
compiler at Compaq SRC. Results on new javac are below the program. Note
that we do not complain if an anonymous class has a static member, contrary
to the specs. Old javac does (though it misses all the other errors!).
I don't really know why we prohibit static members in inner classes anyway.
It seems pointless. Does anyone know of a reason? However, if it is
convenient, I don't mind keeping the prohibition too much. We do need to
enforce it consistently.
/**
** Test a corner case of Java 1.1: local class declarations contained
** in static members.
**/
class LocalClassInStaticMember {
int i = 0;
static int s = 1;
static void m() {
final int l = 3;
class Local { // Enclosing instances: none
int f = i; // error: outside instance fields not accessible
int g = s; // static fields are ok, though
int x = l; // ditto outside final local variables
static int w=1; // error: local types can't contain static members
class Inner { // Enclosing instances: Local
int q = f; // we have a Local enclosing instance so ok...
int r = i; // error: still can't access i field
}
}
Local e = new Local(); // ok since don't need an enclosing instance
class Local2 extends Local {
Local2() {} // ok for same reason
}
}
}
//* Basically same test, but using anonymous instead of block-level classes:
class LocalClassInStaticMember2 {
int i = 0;
static int s = 1;
static void m() {
final int l = 3;
Object o = new Object() { // Enclosing instances: none
int f = i; // error: outside instance fields not accessible
int g = s; // static fields are ok, though
int x = l; // ditto outside final local variables
static int w=1; // error: local types can't contain static members
class Inner { // Enclosing instances: <anonymous>
int q = f; // we have a <anony.> enclosing instance so ok...
int r = i; // error: still can't access i field
}
};
}
}
/usr/local/java/jdk1.3/solaris/bin/javac LocalClassInStaticMember.java
LocalClassInStaticMember.java:19: non-static variable i cannot be
referenced from a static context
int f = i; // error: outside instance fields not accessible
^
LocalClassInStaticMember.java:24: inner classes cannot have static declarations
static int w=1; // error: local types can't contain static members
^
LocalClassInStaticMember.java:29: non-static variable i cannot be
referenced from a static context
int r = i; // error: still can't access i field
^
LocalClassInStaticMember.java:59: non-static variable i cannot be
referenced from a static context
int f = i; // error: outside instance fields not accessible
^
LocalClassInStaticMember.java:69: non-static variable i cannot be
referenced from a static context
int r = i; // error: still can't access i field
^
5 errors
Cheers, Gilad
william.maddox@Eng 1999-10-07
Here's another test case I got from Mark Lillibridge, whose doing a Java
compiler at Compaq SRC. Results on new javac are below the program. Note
that we do not complain if an anonymous class has a static member, contrary
to the specs. Old javac does (though it misses all the other errors!).
I don't really know why we prohibit static members in inner classes anyway.
It seems pointless. Does anyone know of a reason? However, if it is
convenient, I don't mind keeping the prohibition too much. We do need to
enforce it consistently.
/**
** Test a corner case of Java 1.1: local class declarations contained
** in static members.
**/
class LocalClassInStaticMember {
int i = 0;
static int s = 1;
static void m() {
final int l = 3;
class Local { // Enclosing instances: none
int f = i; // error: outside instance fields not accessible
int g = s; // static fields are ok, though
int x = l; // ditto outside final local variables
static int w=1; // error: local types can't contain static members
class Inner { // Enclosing instances: Local
int q = f; // we have a Local enclosing instance so ok...
int r = i; // error: still can't access i field
}
}
Local e = new Local(); // ok since don't need an enclosing instance
class Local2 extends Local {
Local2() {} // ok for same reason
}
}
}
//* Basically same test, but using anonymous instead of block-level classes:
class LocalClassInStaticMember2 {
int i = 0;
static int s = 1;
static void m() {
final int l = 3;
Object o = new Object() { // Enclosing instances: none
int f = i; // error: outside instance fields not accessible
int g = s; // static fields are ok, though
int x = l; // ditto outside final local variables
static int w=1; // error: local types can't contain static members
class Inner { // Enclosing instances: <anonymous>
int q = f; // we have a <anony.> enclosing instance so ok...
int r = i; // error: still can't access i field
}
};
}
}
/usr/local/java/jdk1.3/solaris/bin/javac LocalClassInStaticMember.java
LocalClassInStaticMember.java:19: non-static variable i cannot be
referenced from a static context
int f = i; // error: outside instance fields not accessible
^
LocalClassInStaticMember.java:24: inner classes cannot have static declarations
static int w=1; // error: local types can't contain static members
^
LocalClassInStaticMember.java:29: non-static variable i cannot be
referenced from a static context
int r = i; // error: still can't access i field
^
LocalClassInStaticMember.java:59: non-static variable i cannot be
referenced from a static context
int f = i; // error: outside instance fields not accessible
^
LocalClassInStaticMember.java:69: non-static variable i cannot be
referenced from a static context
int r = i; // error: still can't access i field
^
5 errors
Cheers, Gilad
william.maddox@Eng 1999-10-07
- relates to
-
JDK-4401797 Compiler can't handle static final arrays in inner classes
-
- Closed
-
-
JDK-6510286 Wording of javac error for inner classes
-
- Closed
-