Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Java - turning a string into a variable (equiv to PHP's $$)?
Closed Thread
Old 08-18-2006, 04:34 PM   #1 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default Java - turning a string into a variable (equiv to PHP's $$)?

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
__________________
Vormund is offline  
Old 08-18-2006, 04:50 PM   #2 (permalink)
 
Monster Techie

Join Date: Apr 2005

Posts: 1,909

jcortes is on a distinguished road

Send a message via AIM to jcortes
Default

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

$var = "Mike";
echo $var; // outputs: mike.
__________________

AIM = jcortestechhelp
jcortes is offline  
Old 08-18-2006, 04:56 PM   #3 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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).
__________________
Vormund is offline  
Old 08-18-2006, 05:31 PM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

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?
jaeusm is offline  
Old 08-18-2006, 05:43 PM   #5 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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!
__________________
Vormund is offline  
Old 08-18-2006, 06:03 PM   #6 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

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?
jaeusm is offline  
Old 08-19-2006, 05:58 PM   #7 (permalink)
 
Newb Techie

Join Date: Jul 2006

Posts: 34

The Future

Send a message via AIM to The Future
Default

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.
The Future is offline  
Old 08-20-2006, 02:26 AM   #8 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

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!
__________________
Vormund is offline  
Old 08-20-2006, 11:52 PM   #9 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default

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.
jaeusm is offline  
Old 08-20-2006, 11:54 PM   #10 (permalink)
 
Monster Techie

Join Date: May 2004

Location: Tucson, AZ, USA

Posts: 1,183

Vormund

Send a message via AIM to Vormund Send a message via MSN to Vormund Send a message via Yahoo to Vormund
Default

Nope, hehe, it was more of to see if it's possible...but thanks for the info...
__________________
Vormund is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On