3

Possible Duplicate:
Generics compiles and runs in Eclipse, but doesn't compile in javac
Compilers behave differently with a null parameter of a generic method

public static void main(String[] args) {
    Class<Object> type1 = String.class;
    Class<? extends Object> type2 = String.class;
    Class<Object> type3 = get(String.class);
    Class<Object> type4 = Foo.<Object, String> get(String.class);
}

public static <I, C extends I> Class<I> get(Class<C> type) {
    return null;
}

The first line will generate the following compilation error:

Type mismatch: cannot convert from Class<String> to Class<Object>

which is pretty much normal. The second line will work fine.

The third line works differently when compiling in eclipse and in javac. Eclipse will compile happily. Javac will give me the following error:

[ERROR] Foo.java:[36,26] incompatible types
[ERROR] found   : java.lang.Class<java.lang.String>
[ERROR] required: java.lang.Class<java.lang.Object>

The forth line compiles correctly in both compilers.

Can anyone explain what is happening? Thank you :)

Community
  • 1
  • 1
ebelanger
  • 701
  • 7
  • 11
  • What's with the two `main` methods? The second `main` seems to be identical to the third line of the first `main`. Also, what version of javac are you using? – Ted Hopp Nov 03 '11 at 19:43
  • really sorry about that second main. my bad. and the javac version is 1.6.9 – ebelanger Nov 03 '11 at 19:49
  • Have you tried `Class type3 = Foo.get(String.class)`? I think the compiler is creating the following `Class type3 = Foo.get(String.class);` – John B Nov 03 '11 at 19:52
  • what's your eclipse compiler compliance level settings? – Puran Nov 03 '11 at 19:54
  • @BalusC this is not a duplicate of either of prev questions linked. especially the 1st question which you answered, you should have known that the question is very different. – irreputable Nov 03 '11 at 22:18
  • @irreputable: I haven't proposed the 1st as dupe, but the 2nd. Someone else did. – BalusC Nov 03 '11 at 23:41

0 Answers0