File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to get the class name of an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to get the class name of an object " Watch "How to get the class name of an object " New topic
Author

How to get the class name of an object

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
1. If I have an object handle, can I use "obj.getClass().getName()" to find out the class name of this object ?

2. If I create a class called "Fruit", somewhere in the code if I want to get the class name, can I do "(Fruit.class).getName()" to get it ?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

If you have a compiler and JRE, why don't you try it in a small program and let us know what is the result?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Have you tried it? I think both your methods will work, but haven't tried for a long time.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
The developers of Java were nice enough to document their code, including the Class class.

Next time, pleas...
http://faq.javaranch.com/java/ShowSomeEffort


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
I tried both of them before I posted the question. I wouldn't just paste something without testing. However, since the code came from my guess, so I was not sure if that's the standard way to do and would like to see how others do.
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
If you know the name (Fruit.class), why would you want to get the name? It's like saying: What is your name Ben?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by ben oliver:
I tried both of them before I posted the question. I wouldn't just paste something without testing. However, since the code came from my guess, so I was not sure if that's the standard way to do and would like to see how others do.


Since it was not mentioned, it was assumed that might have been lack of efforts!
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by ben oliver:
I tried both of them before I posted the question. I wouldn't just paste something without testing. However, since the code came from my guess, so I was not sure if that's the standard way to do and would like to see how others do.


Thats great!

Yes your option 1 is right and thats the way you obtain the classname. Be aware that it will give you the fully qualified class name including its package name.

For your option2, Petrus has answered. But still, you can get the FQCN with this classname what you have!

If you invoke it with



you may get the output as


com.test.pkgname.MainClass


provided the MainClass is a part of the package "com.test.pkgname".

Does that help?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get the class name of an object
 
Similar Threads
Get Exception Name?
how to obtain the name of an instance class
Use objects in other classes
Interview Question
what is servlet context interface implementation class name