Name: rm29839 Date: 11/04/97
1. Note that this construct works correctly in IBM VisualAge for Java.
2.public interface Test2 {
public static final Class x = String.class;
}
3.
Test2.java:1: Interface methods can't be native, static, synchronized, final, private, or protected : java.lang.Class class$(java.lang.String)
public interface Test2 {
^
Test2.java:2: Can't make static reference to method java.lang.Class class$(java.lang.String) in interface Test2.
public static final Class x = String.class;
^
Note that this will
work if the declaration is embedded in a class declaration as in
public class DoesWork {
public static final java.lang.Class classOfString = String.class;
}
Ronan,
Let me try this again. What I am doing is NOT a usage error.
Let's take this a step at a time.
1. In Java 1.1 (this is a new feature in 1.1) for any class X, the
expression X.class is the Class object for X. In particular String.class is
the Class object for the String class and int.class is the class object for
the primitive type int (see the Inner Classes and other new language
features for the details).
2. A Java expression can be assigned to a variable of the same type. In
particular, the declaration
java.lang.Class classOfString = String.class;
is valid (I gave a fully qualified classname to emphasize that I mean Class
and not class here).
3. It follows, that the declaration
public static final java.lang.Class classOfString = String.class;
is also valid.
4. It follows that the interface declaration
public interface DoesNotCompile {
public static final java.lang.Class classOfString = String.class;
}
is also valid.
But if you try to compile this it fails with javac. Note that this will
work if the declaration is embedded in a class declaration as in
public class DoesWork {
public static final java.lang.Class classOfString = String.class;
}
I have now reread section 9.3 of the Java Language Specification on field
declarations in interface and this confirms the correctness of my reasoning.
This is **clearly** a compiler bug. It relates to how the compiler has
decided to implement a particular language feature by using a
source-to-source transformation (i.e., it generates a hidden static method
to get the class object, but interfaces are not allowed to have static
methods, hence the error messages). That the VisualAge compiler handles
this is of course no proof, I just stated this as a fact. The proof is the
above derivation.
Note that I have used String only as an example. I am not trying to extend
String, I needed the above in an interface to hide the actual class to be
used for a certain purpose and decouple this decision from the users of the
interface. Note that for strings in particular there might be a workaround
(I have not tried this), i.e., to use
Class classOfString = "".getClass();
However this is a special case and cannot be generalized to all classes.
(Review ID: 18583)
======================================================================
- duplicates
-
JDK-4093023 Class Literals: interfaceI{Class c=I.class;}, causes errors and should not
-
- Closed
-