Solved

Java object reference from string

Posted on 2006-07-17
2
264 Views
Last Modified: 2010-04-17
In some languages you can use a string as an object reference.

For example, in ActionScript:

var foo = "bar";
_global[foo] = "baz";

Is the same as saying

_global.bar = "baz";

Is this possible in Java, and if so what's the syntax?

Thanks,
Matt.
0
Comment
Question by:mb2297
2 Comments
 
LVL 15

Accepted Solution

by:
bpmurray earned 50 total points
ID: 17124319
No, you can't do that. I think you're looking for something to manage name-value pairs, where you want to grab the value associated with a name. In the example above, the name is "bar" and the value is "baz". If that's the case, you can use java.util.map:

    String myKey = "bar";
    String myValue = "baz";
    myMap.put(myKey, myValue);


    String val = (String) myMap.get(myKey);
0
 

Author Comment

by:mb2297
ID: 17124389
Using a Map was my backup plan. Thanks bpmurray
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question