What causes "incompatible conditional operand" when using instanceof in Java?
Solution 1
instanceof
used to check that given object is of type Class(SomeClass - right side parameter). You cannot use Class
to check that instanceof another class. To check class equality or assainability you can use SomeClass.isAssignableFrom(clas)
Solution 2
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
Reference variable clas
is not a type of SomeClass
or its sub-class.
user1016403
Updated on June 04, 2022Comments
-
user1016403 almost 2 years
Possible Duplicate:
instanceof - incompatible conditional operand typesI am trying to use below code and getting compilation error.
Class<A> clas; //this is passed from service clas instanceof SomeClass
This gives ma the following compilation error:
incompatible conditional operand types Class and SomeClass
Please help me!