It's not a secret anymore!
The moose likes Beginning Java and the fly likes How to get Class Name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to get Class Name?" Watch "How to get Class Name?" New topic

How to get Class Name?

Kathiresan Chinnasamy
Ranch Hand

Joined: Jan 01, 2008
Posts: 65
Hi friends,

I have an Object.. I have to find out the corresponding class name for that object..
Is there any method available?

Can anyone help me?

Thank you in advance
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Call getClass() on the object (a method inherited from java.lang.Object that returns a java.lang.Class); then call getName() on the Class; so

String className = object.getClass().getName();

[Jess in Action][AskingGoodQuestions]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
A suggestion on how to answer questions of that type:
read the API doc.
For example, use the Index (top, to right of center on the Java API page) for a method like getClass and the see how to use that method and what it returns. Then look at what it returns and see if there is any help there.
pete stein

Joined: Feb 23, 2007
Posts: 1561
You've been given good advice here, but I have to ask you: why do you want this information? There are times when it reflection be helpful, but there are other times where it is inappropriate and can even suggest design flaws. Perhaps you really don't need to know this, and perhaps there is another way to solve your current issue. So, if you come back to this thread, please let us know some background if possible, and many here will be able to tell you if your use of reflection is necessary or not.
I agree. Here's the link:
subject: How to get Class Name?
It's not a secret anymore!