Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

build variable-names out of strings

Status
Not open for further replies.

bateman23

Programmer
Mar 2, 2001
145
DE
Hi,

is there any posibility in java to build variable names out of strings at runtime?
I got and array of values and want to create variables out of them:
Code:
[b]The Array:[/b]
String my_array[] = new String[] {"one", "two"};
[b]And i need to build this two variables:[/b]
error_one="Value";
error_two="Value";
..where the parts "one" and "two" are taken out of the array.
Can this be done in java?

despite crossposting is not welcome, i'm posting this question in the java-forum.
In the jsp-forum nobody seems to know an answer.
Anyway: For more background information you can find the thread at:

Hopefully somebody can give me some hints or an answer. Any help would be greatly appreceated. Thank you very much in advance,
Daniel
 
No...

What's wrong with
Code:
my_array[0] = "Value";
my_array[1] = "Value";
?

Variable names are trashed by the compiler anyway. During runtime, a variable name means nothing.

-Bones
 
bateman23 - I showed you the closest representation of your PHP script to what it would look like in JSP/Java - as Bones3 says - you cannot build variables at runtime - its not a scripting language !

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks to both of you. So it seems i have to think about using arrays instead.. and trying to forget my scripting-style of thinking :)
But anyway i'll miss the feature of building vars at runtime...

---------------------------------------
Visit me @:
 
<A little OT, cause it's not really related to Java>
IMO - that particular `scripting-style' of thinking is a pretty dangerous thing to adopt - and this is coming from a Perl fan. Whether the language lets you or not, it's generally much safer to keep related variables in a related data structure (such as an Array or a Hashtable), rather than dynamically creating unrelated datastructures on the fly to hold related data. The risk of clobbering variables elsewhere in your program is just too high - and it's really, really difficult to track down the bug if/when it all goes horribly wrong (this is exactly the reason that the PHP folks decided to turn off register_globals by default).

There's a good article on it here. It's written about Perl, but the concepts apply to other languages too.
 
Very nicely written article. And despite it doesn't exactly match my situation it makes things just even more clear: Although making things at first glance a lot easier, dynamic named vars are not the best thing to use...
I hope i'm able to solve the problem in another way (like arrays) and don't regret the sweet poison of "scipting style" to long :)

---------------------------------------
Visit me @:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top