if i have a method defined.
public static <T> T getSomeTea(Class<T> returnType) {
//do stuff and return a T
}
public static <T> T getSomeSpecialTea(T someVal) {
T someTea = (T) getSomeTea(someVal.getClass());
}
in getSomeSpecialTea, why do I need to cast the return from getSomeTea. it would seem to me the cast is unneccesary, but perhaps i’m missing something important.
Object.getClass()isn’t generic, so your code is effectively:At that point is it clearer why you need the cast? (Not that the cast will really anything at execution time, of course…) You’re not calling
<T>getSomeTea()which is what you really want to be doing, effectively.It’s just another case of Java generics being a bit of a pain 🙁