Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a variable from a calling scope inside a method

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Inside the foo method, I want to be able to access the variable _m without passing it in as a parameter. One way is to add _m into a ThreadLocal. Are there any other ways ?

Thanks

Pho
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could make it a static variable of the class. But first I'd ask you why it can't be a parameter.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be a non-static class variable, too. And yes, I would ask the same question as Nick did.
 
krishna pradeep
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First,it can be a parameter............
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason why I don't want it to be a parameter is purely aesthetic.
Basically foo() is called from within a method. My goal is to be able to identify in which method foo was called from. So currently I am indicating that by an integer. Another option would be to somehow identify my enclosing method by reflection or something.

Thanks.

Pho
 
Kashif Iqbal Khan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare an Exception Object and use the Exception.getStackTrace() and examine the StackTraceElement to know the caller method
[ June 09, 2005: Message edited by: Kashif Iqbal Khan ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
Basically foo() is called from within a method. My goal is to be able to identify in which method foo was called from.


That's sounds generally like a bad idea to begin with. Can you tell us more about why you want to do that?
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if your only reason for wanting to find out the calling method is to get at that integer, and your only use for the integer is to identify the calling method, then you definitely want either reflection or the Exception.getStackTrace() trick. probably the latter.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Beck:
then you definitely want either reflection or the Exception.getStackTrace() trick. probably the latter.


Except that, as far as I know, it isn't guaranteed to work.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Except that, as far as I know, it isn't guaranteed to work.


well, drat, that means no method i know of is guaranteed to do this. i recommended getStackTrace() over reflection because, one, reflection is a large and complex additional API to worry about whereas exceptions are around in any event; and, two, security managers can at times override parts of the reflection API, including for all i know perhaps the part that would help with this. but if getting a stack trace from an exception might randomly fail, too, then i dunno how to do this reliably.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by M Beck:
well, drat, that means no method i know of is guaranteed to do this.


As far as I can tell, that is because there is none...

It's a bad design idea, anyway - it provides a type of coupling that you simply should try to avoid. If at all, it's good for debugging kind of things, such as logging - and for those, it doesn't hurt that much that it isn't guaranteed to work reliably...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic