-1

Possible Duplicate:
Get class of generic

I'm trying to do this:

public static<T extends Object> T perform(){
    Class<T> classObject = T.class;
    Class<T> classObjectAgain = T.getClass();
    //...
}

I thought the "extends Object" would bring me access to .class and getClass()... However this doesn't compile: "Illegal class litteral"

Do you know how I could perform such an operation ?

Community
  • 1
  • 1
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
  • There are lots of duplications for this answer, just search stackoverflow more. like "getting class from generic type". – d1e Apr 17 '12 at 15:47

1 Answers1

1

Java Generics are implemented through type erasure (since they only appeared in Java 5, this type of implementation was chosen in order to support legacy code).

This means that at runtime, all the generic types are in fact of type Object. So you cannot use reflection to obtain information about parametrized objects at runtime.

Random42
  • 8,989
  • 6
  • 55
  • 86