0

Is there a way to check what class you passed to a parameterized function using something like instanceof? In some ways this kind of defeats the purpose (I get that), but I was wondering if something like this was possible.

public static <T> List<T> foo(String parseMe) {
    List<T> lst = new ArrayList<T>();
    if (T instanceof Type1) {
        ...
    } else if (T instanceof Type2) {
        ...
    } else {
        ...
    }
    return lst;
}
fish
  • 441
  • 2
  • 6
  • 15
  • No, as Java's impl of generics is based on type-erasure. See answer and solution to your problem in: http://stackoverflow.com/questions/3437897/how-to-get-class-instance-of-generics-type-t – Kenston Choi Apr 05 '15 at 10:16
  • No, see [type erasure](https://docs.oracle.com/javase/tutorial/java/generics/erasure.html). – Boris the Spider Apr 05 '15 at 10:22

0 Answers0