Is the class T in the generic class <T> assignable from another class?
Edit Because there were many downvotes and people who did not understand what I was asking, I will rephrase:
How to find out at runtime what is the class that foo was generated?
public boolean doesClassImplementList(Class<?> genericClass) { // help me fill this in // this method should return true if genericClass implements List // I can't do this because it doesn't compile: return genericClass instanceof List; } You must use Class.forName ("full_qualified_name") to create a Class object or using the .getClass () method for an already created object. In addition, for int types, the class is defined in the Integer.TYPE property.
I need to clarify a little what you are trying to do, map the same type? You can use several methods, what is the ultimate goal?
Ok, now that you have clarified ... here's some help ... just go to class ..
public boolean doesClassImplementList(Class genericClass) { // help me fill this in // this method should return true if genericClass implements List // I can't do this because it doesn't compile: //this will give you aa list of class this class implements Class[] classesImplementing = genericClass.getInterfaces(); //loop through this array and test for: for(int i=0; i<classesImplementing.length; i++) { if (classesImplementing[i].getName().equals("java.util.List")) { return true; } } return false; } Class<Integer> is a type declaration, you cannot assign it to a variable, as you do in your first line.
Class<Integer> is basically a way to access the Integer type, so you can pass it as a parameter. This affects some methods (for example, casting), but basically it is a way to provide a general parameter to a method and associate it with a type.
For instance:
public <T> T someMethod(Object someObject, Class<T> castTo) { return castTo.cast(someObject); }