Java example to get Object class name at runtime

In java there is a way that makes us enabled to get the object class name at runtime.

Java example to get Object class name at runtime

In java there is a way that makes us enabled to get the object class name at runtime.

Java example to get Object class name at runtime

Java example to get Object class name at runtime

     

java get Object class name

In java there is a way that makes us enabled to get the object class name at runtime. It can be done by calling the getClass() method on the class object. Then  by calling the method getName() we can get the name of the object class.

In our example java program we have created a class RoseIndia and we have created an object of this class. Now we will be calling method getClass() on this object to get the class. 

roseIndiaObject.getClass() returns the runtime class.  roseIndiaObject.getClass().getName() gets the class name of the created object at runtime. Here is the full java code of program which will be getting the object's class name as follows:

RoseIndia.java

import java.lang.Class;

public class RoseIndia
{
  public RoseIndia(){
  System.out.println("Constructor Called");
  }
  public static void main(String args[])
  {
  RoseIndia roseIndiaObject = new RoseIndia();
  System.out.println("Object's Class name =>"+
   roseIndiaObject.getClass
().getName());
  }
}

Output:

C:\javaexamples>javac RoseIndia.java

C:\javaexamples>java RoseIndia
Constructor Called
Object's Class name =>RoseIndia

Download Source Code