Java: how to look for instanceof method

Imagine I want to write a useless method: isInstanceofthat returns boolean.
I thought about it. But I do not go out. instanceofshould be used as:

[object] instanceof [a classname]

// I was thinking about something like this
public static boolean isInstanceof(Object obj, /*magic for the second param*/)
{
   return obj instanceof /*the magical second param*/
}

But how can I make a parameter for [a classname]? Is there a way to do this without a method isInstance(Class cls)from java.lang.Class?

thank

+2
source share
2 answers

Hehe yes. Use isAssignableFrom (Class) from Class. This is not only not isInstance(Class), but also how the operator instanceofworks more closely. :)

Other than that, no, you cannot do without these methods from Class.

+5
source

[object] instanceof [a classname] 

:

Class.forName("a classname").isAssignableFrom(object.getClass());
+2

All Articles