I have a variable:
Class<Map.Entry<String, Boolean>> clazz;
And I want to assign a class to it without instantiating anything. but compiler doesn’t let me write:
Class<Map.Entry<String, Boolean>> clazz = Map.Entry<String, Boolean>.class;
how can i do the assignment?
Ahh, the joys of type erasure.
The Java compiler distinguishes between the types
Map.Entry(raw) andMap.Entry<String, Boolean>(parameterized). Unfortunately, you can’t add the type parameters in a type literal using.class. So you have to cast. But you can’t do this directly; you’ll have to take a ‘detour’ throughClass<?>. I don’t remember why, exactly, I’m sorry :).Also, you’ll get an ‘unchecked’ warning, which you can suppress, because you know (in this case) that the cast will always succeed. So:
(No need to put the warning on the method where this assignment happens; you can just put it directly in front of the assignment.)
Enjoy! 🙂