Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-get-example Java example to get Object class name at runtime

Related Tutorials


 
 

Share on Google+Share on Google+

Java example to get Object class name at runtime

Advertisement
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 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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2008

Related Tutorials

Discuss: Java example to get Object class name at runtime   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
naresh pallothu
October 12, 2011
Doubt on getClass()

what is the difference between roseIndiaObject.getClass() returns the runtime class and roseIndiaObject.getClass().getName() gets the class name of the created object at runtime.
Nagaraju GUnji
February 6, 2012
Another way

RoseIndia.class.getName() is another ways.
Ted S
March 19, 2012
get Object class name at runtime

The whole point of getting an object's class name at runtime is when you do not know the object's class name before hand. This example creates a class RoseIndia, then creates an instance of the class, then does the roseIndiaObject.getClass().getName()); So what? Yes, we have shown how to call the method, but this is not how the method is used. The purpose of the method is to getName() of something we don't know the name of. How do we determine an object's class name when we don't know the object's type before runtime! That is the question that needs answered here.
Cruze
July 3, 2012
Java

Can you please further explain: roseIndiaObject.getClass().getName()
Arsalan
October 10, 2012
How can i display the name of calling Object

how can i display the name of calling object eg in the above mentioned case how can i display roseIndiaObject ?? Thanks in advance :)
karthick
January 3, 2013
learning JAVA...!

HI JAVA teams gd eve... has i am learning basic java language what.?
DMCA.com