简体   繁体   中英

Java instanceof with class name

I am just curious to ask this, maybe it is quite meaningless.

When we are using instanceof in java, like:

if (a instanceof Parent){ //"Parent" here is a parent class of "a"
}

why we can't use like below:

if (a instanceof Parent.class){
}

Does the second 'instanceof' make more sense from the view of strict programming? What is the difference between "Parent" and "Parent.class"?

What is the difference between "Parent" and "Parent.class"?

The latter is a class literal - a way of accessing an object of type Class<Parent> .

The former is just the name of a class, which is used in various situations - when calling static methods, constructors, casting etc.

Does the second 'instanceof' make more sense from the view of strict programming?

Well not as the language is defined - instanceof only works with the name of a type, never an expression. If you could write

if (a instanceof Parent.class)

then I'd expect you do be able to write:

Class<?> clazz = Parent.class;
if (a instanceof clazz)

... and that's just not the way it works. On the other hand, there is the Class.isInstance method which you can call if you want.

What do you mean by "the view of strict programming" in the first place?

Parent is a class, so the second example doesn't make more sense that the first. You're asking if the instance is an instance of the class, a instanceof Parent is a pretty direct expression of that.

Parent.class is an instance of Class , so even if the second example compiled (it doesn't, the right-hand of instanceof can't itself be an instance), it wouldn't check what you want it to check. :-)

Parent is the name of a type. Parent.class is essentially a static variable that refers to an object (specifically, an instance of Class ). You want to ask whether a is an instance of the Parent type, not whether it's an instance of an object that is itself an instance of some other type (named Class ).

I am just curious to ask this, maybe it is quite meaningless.

When we are using instanceof in java, like:

if (a instanceof Parent){ //"Parent" here is a parent class of "a"
}

why we can't use like below:

if (a instanceof Parent.class){
}

Does the second 'instanceof' make more sense from the view of strict programming? What is the difference between "Parent" and "Parent.class"?

The static Parent.class member is actually an object. You could assign it to a variable of type Object or type Class if you wanted to:

Object o = Parent.class;
Class c = Parent.class;

Parent on the other hand isn't an object or a variable: it is a Type Name , as per the Java spec.

If you could do this...

a instanceof Parent.class

Since Parent.class is an object then you could feasibly could also do this:

Cat myCat = new DomesticLonghair();
a instanceof myCat;

... which is just silly.

The question is not silly, as suggested above!

Between classes the subclass relation is defined by method Class<?>.isAssignableFrom()

if (Parent.class.isAssignableFrom(a.getClass()))
{
    // a is instance of Parent or subclass
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM