Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-core-dev] [1.5] Special handling for Object.getClass()



FYI

The following program is demonstrating a curious behavior in Javac:

public class X {
    public static void main(String[] args) {
                         X x = new X();
                         Class<? extends X> c1 = x.getClass();  // OK
                         String s = "hello";
                         Class<? extends X> c2 = s.getClass();  // KO
    }
}

X.java:6: incompatible types
found   : java.lang.Class<? extends java.lang.String>
required: java.lang.Class<? extends X>
                Class<? extends X> c2 = s.getClass();
                                                  ^
1 error


Officially the type of Object.getClass() is:  Class<? extends Object>.
It seems that the compiler is automatically transforming it into: Class<?
extends X> for the invocation of x.getClass(). This is what is exposed by
the
subsequent invocation with a String...


Also see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=58666



Back to the top