• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Who invoked this method?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to tell (within the method) which class invoked the method without passing it as a parameter? Thanks.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make it a singleton. It still supplies its name as a parameter but not to the method. Guess this might not be what you want but incase ...

Example
 
Matt Sheehan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Sandeep, but the class I have will have its methods called upon by several different classes and I need to keep some of the data seperate. I guess I can just have the method take a Class as a parameter, but I thought there might be a built-in way to get it.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think hard about why it is your method does different things at different times. Make sure your method names and parameter names make it very clear to the reader why different things happen. Basing logic on who called is probably not a good idea. You want to make methods that can be used by unforseen future objects. All of them will expect the method to behave the same for the same parameters. Gotta run ... lemme know if that made sense.
 
Matt Sheehan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Stan, your comments do make sense but I'm not really doing different things in the method. The reason I'm interested in the class name is that I need to display the data graphically, and I want it to be grouped by class.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question seems to come up alot.
You can get a look at the complete calling stack if using Java 1.4 with the getStackTrace() method in java.lang.Throwable.
Using the array of StackTraceElements returned you can get the class, method and line number. In earlier versions of Java you have to roll-your-own equivalent by capturing the output of printStackTrace()
Bill
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic