Why is Collection<String>.class Illegal?
Posted
by Peter
on Stack Overflow
See other posts from Stack Overflow
or by Peter
Published on 2010-04-30T14:31:17Z
Indexed on
2010/04/30
14:37 UTC
Read the original article
Hit count: 369
java
I am puzzled by generics. You can declare a field like:
Class<Collection<String>> clazz = ...
It seems logical that you could assign this field with:
Class<Collection<String>> clazz = Collection<String>.class;
However, this generates an error:
Syntax error on token ">", void expected after this token
So it looks like the .class operator does not work with generics. So I tried:
class A<S> {}
class B extends A<String> {}
Class<A<String>> c = B.class;
Also does not work, generates:
Type mismatch: cannot convert from Class<Test.StringCollection>
to Class<Collection<String>>
Now, I really fail to see why this should not work. I know generic types are not reified but in both cases it seems to be fully type safe without having access to runtime generic types. Anybody an idea?
Peter Kriens
© Stack Overflow or respective owner