I have a list like that:
private List myList = new ArrayList();
I want to get the .class of T. How can I do that?
Why do you need to know this? Perhaps this will do what you need
// must be at least one element (and all elements are the same)
Class elementType = myList.get(0).getClass();
The only way to do this is to either
like
private final Class type;
private final List list = new ArrayList();
public Class getType() { return type; }
like
List list = new ArrayList() { }; // create a specific sub-class
final Class extends List> listClass = list.getClass();
final ParameterizedType genericSuperclass = (ParameterizedType) listClass.getGenericSuperclass();
Class elementType = (Class) genericSuperclass.getActualTypeArguments()[0];
This has the dis-advantage that you could end up creating lots of anonymous classes and potential confusion with things like ArrayList.class != list.getClass()
however list instanceof ArrayList