How to print an object's implemented class

Vivek picture Vivek · Jan 27, 2012 · Viewed 23.5k times · Source

During debugging without the aid of an IDE(Integrated Development Environment), I would like to determine an object's class. The catch is that the object is defined as an interface and I would like to determine the Object's class that is implementing this interface. So for instance I would like to print statements in the following setter method to print the implemented class name:

public void setSomeObject(InterfaceType someObject) 
{
   m_Object = someObject;
   System.out.println(someObject.getClass().getName());
}

I am in the process of testing this code sample and will provide more feedback on this question. As per docs of the java.lang.Class and java.lang.Object api, I believe the interface name will be printed instead of the class that implemented this interface.

My question is how does one print the name of the implemented class instead of the interface in the above code sample?

Answer

NPE picture NPE · Jan 27, 2012

I believe the interface name will be printed instead of the class that implemented this interface.

That's not correct: getClass().getName() will print the name of the class. The Javadoc is pretty clear on this:

public final Class<?> getClass()

Returns the runtime class of this Object.