File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reading a variable from a calling scope inside a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading a variable from a calling scope inside a method" Watch "Reading a variable from a calling scope inside a method" New topic
Author

Reading a variable from a calling scope inside a method

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761


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

Joined: Apr 04, 2004
Posts: 815
you could make it a static variable of the class. But first I'd ask you why it can't be a parameter.


I've heard it takes forever to grow a woman from the ground
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Could be a non-static class variable, too. And yes, I would ask the same question as Nick did.
krishna pradeep
Greenhorn

Joined: Jun 07, 2005
Posts: 1
First,it can be a parameter............
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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

Joined: Dec 22, 2001
Posts: 13
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 ]

Kashif I. Khan
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 14, 2005
Posts: 323
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

Joined: Jul 11, 2001
Posts: 14112
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a variable from a calling scope inside a method