Question

I have a condition -

public class A {}
public class B extends A {}
A a = new B();



boolean flag = a instanceof B; // returns true

public boolean isOfType(A a, Class<? extends A> type) {
    return (a instanceof type); // throws syntax error
}

I am very curious why the condition is throwing syntax error. Am I doing anything wrong here?

Était-ce utile?

La solution

Yes, you should be using type.isInstance(a). instanceof is not for objects of type Class, but for class names, e.g. a instanceof String, not a instanceof String.class.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top