Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

determine class info at runtime

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I want trap the class name of an incoming object.
Here is the scenario
I have a super class Parent & have 5 derived classes which extend Parent say Child1, Child2, Child3 , Child4, Child5.

Now the child(n) object is sent to me after doing an upcast.
Parent p = new Child1();
It could be an instance of Child2 , or Child3 so on

Now in my code I want to determine what is the type of the Child class object by using the Parent reference (p) as mentioned above.

Is there a way to do that without resorting to the (instanceof) operator.
Say in a scenario where in I dont know what all subclasses does the Parent class have so I can avoid all the numerous if conditions.



Do post your thoughts on the same


Regards
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calling "p.getClass().getName()" will give you a String with the class name.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

"p.getClass().getName()" is an example of reflection.
There is a great tutorial on the sun web site, http://java.sun.com/docs/books/tutorial/reflect/index.html, which you might want to look at for more details. You can do lots of very nice stuff with reflection, like find out what methods and ctors are available of your unknown class.

Niki
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using getClass().getName() will work.
But I still wonder if you are trying to use a wrong way ,if you should implement some logic in every child classes rather than do it using reflection.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic