简体   繁体   中英

How to store a class name in any variable and use it with instanceof operator

  // store the class name in varibale className
    DataType className = ValueOfclassName;
    if(a instanceof className)
            {
                System.out.println("This is instance");
            }

Well, instanceof is about Class , but class name is a String . You can learn more here .

Anyway, can this answer to your question?

A i = new A();
if (i instanceof A) {

}

if you want a String rapresentation of a class you can use:

A.class.getName()

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