3

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!

Community
  • 1
  • 1
user1016403
  • 12,151
  • 35
  • 108
  • 137

2 Answers2

7

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)

Pokuri
  • 3,072
  • 8
  • 31
  • 55
1

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.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186