Compiler Error J0227

It is impossible for an expression of type 'identifier' to be an instance of 'identifier'

The compiler detected that a comparison of two classes using the instanceof operator could never be instances of each other. In order to use the instanceof operator correctly you must compare classes that have some common class lineage. Either change the expression containing the instanceof operator so that it uses a related class and instance for its comparison or remove the expression and compile again.

The following example illustrates this error:

class Simple1{
   //do something meaningful here
}

class Simple2{
   //do something meaningful here
}

class CompClasses{
   Simple1 x = new Simple1();
   public static void main(String args[]){
   
   if(smp.x instanceof Simple2){
      /*error: non related classes cannot be instances of each other*/
   }
}

The following example illustrates the correct usage of the instanceof operator to determine if a class instance is an instance of a specific class:

class Simple1 extends Simple2{
   //do something meaningful here
}

class Simple2{
   //do something meaningful here
}

class CompClasses{
   Simple1 x = new Simple1();
   public static void main(String args[]){
   
   if(smp.x instanceof Simple2){
      // This is OK since 'Simple1' is a subclass of 'Simple2'
   }
}