KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I would expect that from the aspect of compile time as well as from the aspect of runtime it wouldn't be a problem for .getClass() to provide a correctly-typed return value. But I must be wrong. public class _GetClassGenerics2 { static class MyClass { } public static void main(String[] args) { MyClass myInstance = new MyClass(); // here it works Class<? extends MyClass> type = myInstance.getClass(); myMethod(myInstance); } public static <T extends MyClass> void myMethod(T instance) { Class<? extends T> type = instance.getClass(); // java.lang.RuntimeException: Uncompilable source code - incompatible types // required: java.lang.Class<? extends T> // found: java.lang.Class<capture#1 of ? extends _GetClassGenerics2.MyClass> } } EDIT: It doesn't work with Class<T> and Class<? super T> either.
Tags (comma-separated)
Save Edits
Cancel