Why can't I use the variable of type Class in the second argument of the instanceof operation? Example:

Class mClass= SomeClass.class; If(someObject instanceof mClass) Return; 

Ps I forgot to add that I need instanceof because in the condition it is expected that the heir of the specified class will also return true. Is this possible with a variable?

  • What does "not work" mean? Compilation fails? Exception crashes? - Sergey Gornostaev
  • 2
    Because the instanceof operand checks whether an object belongs to the specified instance, not a variable / object. And you compare two objects. It is more correct to compare if(someObject instanceof SomeClass) . - Rootware
  • @SergeyGornostaev compiler does not accept the variable - Yellastro
  • @Rootware so if I can not specify a specific class, I will not be able to use instanceof, right? - Yellastro

3 answers 3

If the class, the object to which you want to check, is not known at the compilation stage - do it like this:

 if (someObject.getClass().equals(mClass)) { } 

The isInstance will still check all the superclasses of your someObject .

 if (mClass.isInstance(someObject)) { } 
  • Damn, I forgot to indicate that I need to check whether an object is not only an instance but also a class heir from a variable - Yellastro
  • And why not isInstance? - Pavel Mayorov
  • @PavelMayorov Yes, also an option. Publish? - Nofate
  • @Nofate by the way, is there a difference, use equals or == in this case? Does java guarantee that at least getClass() and .class will always return the same instance? (Ie I mean all the ways to get a Class object except for its direct creation via new ) - selya
  • @selya, yes, these are all references to Class instances created by the class loader. So for one loader instance will be one. - Nofate

Option 1 Works with type

 if (someObject instanceof SomeClass) return; 

2 variant Works with type instance

 if (SomeClass.class.isAssignableFrom(someObject.getClass())) return; 
  • And why not isInstance? - Pavel Mayorov
  • @PavelMayorov, for clarity. Although if we take exactly the specification, then from instanceof equivalent is isInstance. - Victor Khovanskiy

In general, if someone is also interested, then I came up with an answer to my own question.

 public boolean isInstanceOf(Class fClass) { Class fThis=this.getClass(); while(fThis!=ArhiClasd.class) { if(fThis==fClass) return true; else fThis=fThis.getSuperclass(); } return false; } 

ArhiClass is the very super class to which we are interested in all matches. You can specify at least Object.

  • What is ArhiClasd ? - Nofate
  • @Nofate is the name of the class in whose inheritance tree we are interested in receiving the answer true. For example, there you can put Object, or Employer, if we solve a problem popular in textbooks - Yellastro
  • Then it is necessary to make it into the parameters of the method. - Nofate
  • @Nofate well, I thought so, this is no longer directly related to the topic, since, again, you can write Object there, and this will work in any implementation, only slower - Yellastro