I know that some time has passed since this was set, but yesterday I found out an alternative
We all know what you can do:
if(o instanceof String) {
but what if you donโt know exactly what type of class it should be? you cannot do in general:
if(o instanceof <Class variable>.getClass()) {
as it gives a compilation error.
Instead, here is an alternative - isAssignableFrom ()
For example:
public static boolean isASubClass(Class classTypeWeWant, Object objectWeHave) { return classTypeWeWant.isAssignableFrom(objectWeHave.getClass()) }
Andy Dingfelder Sep 26 '12 at 20:55 2012-09-26 20:55
source share