Question about passing values?

K

Ken Adams

Ok, this might be a bit of a strange question but. Is there anyway that a
function can get a string representation of the variable name that was
passed into the functions parameters. So say you have some code like

int tester =0;

foo(tester);

foo(int value)
{
in here, is there any way to get the string value "tester" from the
value reference?
}
Thanks
Perhaps using objects a guy could use reflection I suppose to do this. Any
comments on that as well.
 
S

Sudsy

Ken said:
Ok, this might be a bit of a strange question but. Is there anyway that a
function can get a string representation of the variable name that was
passed into the functions parameters. So say you have some code like

int tester =0;

foo(tester);

foo(int value)
{
in here, is there any way to get the string value "tester" from the
value reference?
}
Thanks
Perhaps using objects a guy could use reflection I suppose to do this. Any
comments on that as well.

I haven't the foggiest idea why you'd want to do this. To what end? Why
should it matter what the "name" of the variable is in the caller? It
makes no sense and is, quite understandably, next to impossible.
I say that as I'm sure that some people could suggest taking a stack
trace and uncovering the information that way...
As far as reflection is concerned, an Object is an Object by any other
name (apologies to William Shakespeare). Do you see a getName method in
the javadocs for Object?
 
M

Michael Borgwardt

Ken said:
Ok, this might be a bit of a strange question but. Is there anyway that a
function can get a string representation of the variable name that was
passed into the functions parameters.

Nope, absolutely impossible.

Heck, there *ARE NO* variable names at all for local variables if you compile
without debug information.
 
T

Tony Morris

int tester =0;
foo(tester);

foo(int value)
{
in here, is there any way to get the string value "tester" from the
value reference?
}

Indicative of poor design.
No you can't, no you shouldn't want to.
Time to rethink (assuming you thought in the first place).
 
T

Thomas Schodt

Ken said:
int tester =0;

foo(tester);

foo(int value)
{
in here, is there any way to get the string value "tester" from the
value reference?
}

int tester = 0;

foo("tester",tester);

foo(String name,int value) {
// a way to get the string value "tester" from the caller
}

o_O
 
J

Joona I Palaste

Ken Adams said:
Ok, this might be a bit of a strange question but. Is there anyway that a
function can get a string representation of the variable name that was
passed into the functions parameters. So say you have some code like
int tester =0;

foo(int value)
{
in here, is there any way to get the string value "tester" from the
value reference?
}
Thanks
Perhaps using objects a guy could use reflection I suppose to do this. Any
comments on that as well.

Suppose you called foo(3); instead. Now what would your "string value"
be?
 
M

Mike

Actualy, there is an intersting way to do this (assuming that the
method is in the same class as the object, and the object is global),
you could make the variable an Integer, and reference if the
object==tester, this tests the memory location, and since all you are
doing is passing a pointer to the object, it will return true if the
object passed is the same as the tester, in which case you would print
out the specific name taht you choose...

public class VariableTester {
static Integer theOne, theTwo, theThree;
public static void main(String[] args) {
theOne = new Integer(1);
theTwo = new Integer(2);
theThree = new Integer(3);

test(theTwo);
test(theOne);
test(theThree);
}

public static void test(Integer test) {
if(test==theOne)
System.out.println("theOne");
else if(test==theTwo)
System.out.println("theTwo");
else if(test==theThree)
System.out.println("theThree");
}
}

That code does the trick (at least it works)... Honestly, I have to
agree with the other people, I cant really see a reason for doing this
because you have to know what you named them, and then print out the
specific String depding on which memory location is equal. Oh well, I
liked the challange of figuring this out, hope it helps.
 
M

Mike

Sorry, I read the question wrong, although my previous post does work,
you might want to do something more like this for the method:

public static void test(int test) {
if(theOne.intValue()==test)
System.out.println("theOne);
}

This will also create the same output.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top