What causes "incompatible conditional operand" when using instanceof in Java?

10,028

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

From Java Tutorial.

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.

Share:
10,028
user1016403
Author by

user1016403

Updated on June 04, 2022

Comments

  • user1016403
    user1016403 almost 2 years

    Possible Duplicate:
    instanceof - incompatible conditional operand types

    I 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!