Here is a bug (IMO) in all versions of javac. Note that fastjavac does not exhibit this problem, and javac on classes (vs. interfaces) works fine:
class TestClass {
static Class myClass = TestClass.class;
}
class TestClassSibling {
static Class myClass = TestClass.class;
}
public interface TestInterface {
// *see comments below
//static public Class myClass = TestInterface.class;
}
class TestInterfaceSibling {
static Class myClass = TestInterface.class;
}
/*
NOTE: with this line of code "static public Class myClass = TestInterface.class;" in an interface, fastjavac compiles,
and runs, fine.
javac, however, barfs with the following output:
javalite: ~kbern/tmp/uwe> javac TestInterface.java
TestInterface.java:9: Interface methods can't be native, static, synchronized, final, private, or protected : java.lang.Class class$(java.lang.String)
public interface TestInterface {
^
TestInterface.java:11: Can't make static reference to method java.lang.Class class$(java.lang.String) in interface TestInterface.
static public Class myClass = TestInterface.class;
^
2 errors
Stranger still is that if I comment out that line of code, javac compiles fine, even though the "TestInterfaceSibling"
class has the same line of code... BUT, if move TestInterfaceSibling to its own file in the same dir (and make it
"public"), then it too fails, with the same error. Again, in this case fastjavac has no problem, and the code
runs fine.
Note also that even if I make TestInterfaceSibling be an inner class of TestInterface, then the code in
TestInterfaceSibling gives the same compiler error from javac (but not fastjavac).
*/
class TestClass {
static Class myClass = TestClass.class;
}
class TestClassSibling {
static Class myClass = TestClass.class;
}
public interface TestInterface {
// *see comments below
//static public Class myClass = TestInterface.class;
}
class TestInterfaceSibling {
static Class myClass = TestInterface.class;
}
/*
NOTE: with this line of code "static public Class myClass = TestInterface.class;" in an interface, fastjavac compiles,
and runs, fine.
javac, however, barfs with the following output:
javalite: ~kbern/tmp/uwe> javac TestInterface.java
TestInterface.java:9: Interface methods can't be native, static, synchronized, final, private, or protected : java.lang.Class class$(java.lang.String)
public interface TestInterface {
^
TestInterface.java:11: Can't make static reference to method java.lang.Class class$(java.lang.String) in interface TestInterface.
static public Class myClass = TestInterface.class;
^
2 errors
Stranger still is that if I comment out that line of code, javac compiles fine, even though the "TestInterfaceSibling"
class has the same line of code... BUT, if move TestInterfaceSibling to its own file in the same dir (and make it
"public"), then it too fails, with the same error. Again, in this case fastjavac has no problem, and the code
runs fine.
Note also that even if I make TestInterfaceSibling be an inner class of TestInterface, then the code in
TestInterfaceSibling gives the same compiler error from javac (but not fastjavac).
*/
- duplicates
-
JDK-4055017 Class literals don't work in interfaces
-
- Closed
-