简体   繁体   中英

How to get the type of object reference of instance?

Given an interface , an abstract class and a concrete class

interface Interface {
}

abstract class AbstractClass {
}

class C extends AbstractClass implements Interface {
}

I instantiate two instances of my concrete class C like so

Interface a = new C();

AbstractClass b = new C();

System.out.println(getObjectReferenceName(a));// return some.package.Interface

System.out.println(getObjectReferenceName(b));// return some.package.AbstractClass

/* it return the class name of Object refernce */

String getObjectReferenceName(Object o){
    // todo
    return "class name";
}

How can I get the class name of the reference type?

That is -

a's object reference is some.package.Interface .

b's object reference is some.package.AbstractClass .

To get the name try

System.out.println(a.getClass().getInterfaces()[0]);
System.out.println(b.getClass().getSuperclass());

Output:

interface testPackage.InterfaceClass
class testPackage.AbstractClass

If you want to check if an object is an instance of a class, try instanceof .

Edit:

If you want to get the type the variables were declared with, you can use reflection. This works if these are fields, in other words, if they are declared as class fields, no local variables.

System.out.println(Test.class.getDeclaredField("a").getType()); // Test is the name of the main' method class
System.out.println(Test.class.getDeclaredField("b").getType());

a.getClass().getInterfaces() gives you Array that contain object of Class class that represent all interfaces implemented by class of object a.

ieagetClass().getInterfaces()[0].getName() gives you InterfaceClass

b.getClass().getSuperclass().getName() gives you AbstractClass

This might seem like a stupid answer but

// right here
//  |
//  v

Interface a = new C();

Also you've added a hypothetical method that returns the name of the reference type but

static String getReferenceType(Object o) {

    // the reality is o is always Object
    return Object.class.getName();
}

There's not really a situation where you need to do some program logic based on the type of a reference because you always know the type of the reference. It's in your own declaration. The situation where you need to call a method or instanceof operator is when you have it the other way: when you don't know the actual type of the object.

You can go the java.lang.Class to access methods and then invoke them to get different information. If you want to know which class is based to instantiate an object, you could use referenceVariableName.getClass(); to get its super class, referenceVariableName.getClass().getSuperclass() to do that; to know interfaces, then you can use referenceVariableName.getClass().getInterfaces()[0] (the first interface since there are many interfaces maybe).

Try this:

public class ObjectReferenceName {
   Interface a = new C();
   AbstractClass b = new C();
   C c =new C();
   static String getObjectReferenceName(String fieldName) throws NoSuchFieldException{
        return ObjectReferenceName.class.getDeclaredField(fieldName).getType().getName();
   }

   public static void main(String[] args) throws NoSuchFieldException {
       System.out.println(ObjectReferenceName.getObjectReferenceName("a"));
       System.out.println(ObjectReferenceName.getObjectReferenceName("b"));
       System.out.println(ObjectReferenceName.getObjectReferenceName("c"));
   }

}

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