How do you access a generic class object?

How can I access the class object for Generics?

I am currently doing this as follows:

List<String> list= new ArrayList<String>();
list.getClass();

This is normal? Or what should be the way?

+3
source share
2 answers

This will return the same as ArrayList.class. Java generators erase the generic type at runtime; in other words, from a "list" you will never know that the type of an element is String. There's a great FAQ on this in

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

+7
source

, , . , (, /), . , .

, java.lang.reflect.Method , . getGenericReturnType(). java.lang.reflect.Type, Class, ParameterizedType WildcardType, . . , instanceof , , .

, , ( , ). , .

+3

All Articles