Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Variable names

Thread view: 
steve_marjoribanks@hotmail.com - 02 Mar 2006 18:12 GMT
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
James McGill - 02 Mar 2006 18:22 GMT
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.
Oliver Wong - 02 Mar 2006 18:35 GMT
> 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
Gordon Beaton - 02 Mar 2006 18:42 GMT
> 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

Signature

[  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

steve_marjoribanks@hotmail.com - 02 Mar 2006 19:55 GMT
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
Hendrik Maryns - 02 Mar 2006 20:42 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

steve_marjoribanks@hotmail.com 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.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.