Java - turning a string into a variable (equiv to PHP's $$)?

Status
Not open for further replies.

Vormund

Daemon Poster
Messages
1,178
Location
USA
Does anyone know if this is possible, and if so--how--to turn a string into a variable, using Java? An example in PHP is:

$var = "name";
$$var = "Mike";

echo $name; // outputs: Mike

Thanks:cool:
 
the way you make a variable in php is like this. using your example.

$var = "Mike";
echo $var; // outputs: mike.
 
My example is a way of using a string for a variable name, in PHP. I want to know if it's possible to do the same in Java (IE: maybe someone can make a similar example, if they're really nice:)).
 
Why does it matter what the variable name is? It's only for internal use. So, I guess the question is, why do you want this functionality?
 
Well...for an example, I want a bunch of similar items on a page...so first off, I'd need to loop through (writing PHP examples, since I obviously don't know how in Java):

$list = array( "uno", "dos", "tres" );

for ($i=0; $i<count($list); $i++) {
$string = "item".$i;
$$string = new Object();
}

Now it has defined three objects, and in order to add a new object...I simply add another to the array. (Note--the array values are not used, but it's relating to them by the value... $list[$i]=name.)

It's not the most logical piece of code...but it's not supposed to be. I'm just wanting to know if Java is capable of such things!:)
 
I'm still not quite clear on what you're doing, as I'm not a PHP expert -- I just look up PHP info as I need it. I guess what you're trying to do is create a collection of objects that you can reference by name instead of an index number. If that's the case, you can use a hash table (or HashMap) in Java. Hashmaps contain (key, value) pairs.

Code:
String[] names = {"uno", "dos", "tres"};
HashMap map = new HashMap();
for (int i=0; i<names.length; i++)
    map.put(names[i], new Object());

Is this what you had in mind?
 
I think what you're asking is - is it possible in java to name a variable with the text of a string, and if that is your question, I'm afraid the answer is, to the best of my knowledge, no.
 
Yup, I want what The Future described--

jaeusm, that wouldn't work because I'm declaring globals that I'll need to read values from later, on a web interface... such as...

private TextEntry item1;
private TextEntry item2;
private TextEntry itemn;
private TextEntry item10;

etc. :) Just curiousity.

Thanks for the input though.

If anyone has any additonal ideas/comments, let me know!

Thanks!
 
The Future is correct -- you cannot create a variable from the contents of a string. However, you can use a hash table to do essentially exactly what you're asking for. It would work, but it's not really the best design strategy. Conceptually, all your "variables" would be stored as a collection. So, your variable names, which are the contents of a string, are the keys. The values are the actual variable values. Each "variable" would be referenced from the hash table (the collection), using the appropriate key.

It can be done, but it's definitely not the best route to take. If you find yourself needing to do this, the best option is to redesign your code, as this is more trouble than it's worth.
 
Status
Not open for further replies.
Back
Top Bottom