instanceof
is used used for runtime determination of an object's type. You are trying to determine if x
is really an object of type J
when the program is running, so it compiles.
Were you thinking it should result in a compile-time error because you think the compiler does not know x
's type?
Edit
As Kirk Woll has commented (thanks Kirk Woll!), if you were checking if x
is an instanceof
a concrete class, and the compiler can determine x
's type, then you will get an error at compile time.
From the Java Language Specification:
If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.
As an example of this:
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
class SerializableClass implements Serializable
{
private writeObject(ObjectOutputStream out) {}
private readObject(ObjectInputStream in) {}
}
public class DerivedSerializableClass extends SerializableClass
{
public static void main(String[] args)
{
DerivedSerializableClass dsc = new DerivedSerializableClass();
if (dsc instanceof DerivedSerializableClass) {} // fine
if (dsc instanceof Serializable) {} // fine because check is done at runtime
if (dsc instanceof String) {} // error because compiler knows dsc has no derivation from String in the hierarchy
Object o = (Object)dsc;
if (o instanceof DerivedSerializableClass) {} // fine because you made it Object, so runtime determination is necessary
}
}