SSaMKJ
SSaMKJ

Reputation: 23

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.

Upvotes: 0

Views: 7269

Answers (5)

Sandeep Koparde
Sandeep Koparde

Reputation: 34

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"));
   }

}

Upvotes: 0

htlbydgod
htlbydgod

Reputation: 340

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).

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34166

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());

Upvotes: 3

Radiodef
Radiodef

Reputation: 37855

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.

Upvotes: 0

Mohammad Ashfaq
Mohammad Ashfaq

Reputation: 1375

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

i.e. a.getClass().getInterfaces()[0].getName() gives you InterfaceClass

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

Upvotes: 0

Related Questions