• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to get Class Name?

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Rancher
Posts: 5096
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic