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
Output:
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.