Question about passing values?

Discussion in 'Java' started by Ken Adams, Nov 12, 2004.

  1. Ken Adams

    Ken Adams Guest

    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.
     
    Ken Adams, Nov 12, 2004
    #1
    1. Advertising

  2. Ken Adams

    Sudsy Guest

    Ken Adams wrote:
    > 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?

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
     
    Sudsy, Nov 13, 2004
    #2
    1. Advertising

  3. Ken Adams wrote:

    > 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.
     
    Michael Borgwardt, Nov 13, 2004
    #3
  4. Ken Adams

    Tony Morris Guest

    > 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).

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Nov 13, 2004
    #4
  5. Ken Adams wrote:

    > 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
     
    Thomas Schodt, Nov 13, 2004
    #5
  6. Ken Adams <> scribbled the following:
    > 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.


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

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "It's not survival of the fattest, it's survival of the fittest."
    - Ludvig von Drake
     
    Joona I Palaste, Nov 13, 2004
    #6
  7. Ken Adams

    Mike Guest

    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.
     
    Mike, Nov 13, 2004
    #7
  8. Ken Adams

    Mike Guest

    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.
     
    Mike, Nov 13, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    25
    Views:
    966
    E. Robert Tisdale
    Mar 23, 2005
  2. Chad
    Replies:
    9
    Views:
    369
    David Thompson
    Jan 7, 2010
  3. Angi

    passing values Newbie question

    Angi, Nov 2, 2004, in forum: Javascript
    Replies:
    8
    Views:
    163
    Michael Winter
    Nov 6, 2004
  4. Paul
    Replies:
    1
    Views:
    107
    McKirahan
    Sep 20, 2005
  5. Replies:
    2
    Views:
    181
    Richard Cornford
    Aug 27, 2007
Loading...

Share This Page