Variable names

Discussion in 'Java' started by steve_marjoribanks@hotmail.com, Mar 2, 2006.

  1. Guest

    Sorry if this is a stupid question I am a Java newbie! What I want to
    know is that is it possible to somehow pass a variable name (a string)
    to a method as an argument and then use that as a variable name?

    I've had a think about it and can't think of a way to do it so I'm not
    sure it can even be done in Java?

    Thanks

    Steve
     
    , Mar 2, 2006
    #1
    1. Advertising

  2. James McGill Guest

    The concept you're looking for is the "Reflection" API, and you don't
    want to go anywhere near it as a newbie. Even with this, I don't think
    you can do what you described, exactly.

    Rethink your approach to your problem.
     
    James McGill, Mar 2, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    > Sorry if this is a stupid question I am a Java newbie! What I want to
    > know is that is it possible to somehow pass a variable name (a string)
    > to a method as an argument and then use that as a variable name?
    >
    > I've had a think about it and can't think of a way to do it so I'm not
    > sure it can even be done in Java?


    It can't be done "directly" in the same way that, say, PHP might be able
    to do it.

    You can fake this sort of behaviour by using a HashMap though.

    - Oliver
     
    Oliver Wong, Mar 2, 2006
    #3
  4. On 2 Mar 2006 10:12:31 -0800, wrote:
    > Sorry if this is a stupid question I am a Java newbie! What I want
    > to know is that is it possible to somehow pass a variable name (a
    > string) to a method as an argument and then use that as a variable
    > name?


    Data structures like HashmMap or Hashtable do what you are asking by
    mapping keys (such as Strings) to arbitrary values.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Mar 2, 2006
    #4
  5. Guest

    Ok, thanks people, I thought it might be like that. I've changed my
    code a bit and split the method into two separate methods and used
    'return' to return the int back to the variable name as opposed to
    passing the name to the int. I think I'm slowly getting my head round
    these things!

    Thanks anyhow!

    Steve
     
    , Mar 2, 2006
    #5
  6. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1
    NotDashEscaped: You need GnuPG to verify this message

    uitte de volgende tekst op 03/02/2006
    07:12 PM:
    > Sorry if this is a stupid question I am a Java newbie! What I want to
    > know is that is it possible to somehow pass a variable name (a string)
    > to a method as an argument and then use that as a variable name?
    >
    > I've had a think about it and can't think of a way to do it so I'm not
    > sure it can even be done in Java?


    Sounds like you are in need of the Command pattern?
    H.
    --
    Hendrik Maryns

    ==================
    www.lieverleven.be
    http://aouw.org
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)

    iD8DBQFEB1iciOEY3xKMFEERAt0HAKCEADm3pdX+eB4sB5eHP0fJ7wn+fACeLB0F
    NlYuoAx7ZlEtO5ntgv4jILQ=
    =8aSZ
    -----END PGP SIGNATURE-----
     
    Hendrik Maryns, Mar 2, 2006
    #6
    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. Paddy McCarthy
    Replies:
    3
    Views:
    741
    Anthony J Bybell
    Sep 24, 2004
  2. Bob
    Replies:
    1
    Views:
    405
    Lucas Tam
    Jul 30, 2004
  3. wanwan
    Replies:
    3
    Views:
    450
    Alex Martelli
    Oct 14, 2005
  4. News123
    Replies:
    2
    Views:
    475
    John Machin
    Nov 26, 2008
  5. Peter Buckley
    Replies:
    5
    Views:
    219
    matt neuburg
    Feb 27, 2009
Loading...

Share This Page