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
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: 375
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: 3355

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: 39834
    
  28
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: 375
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: 3355

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: 3355

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