Search code examples
javaidentifier

Using Variable Value as Another Primitive Variable's Identifier


There are a number of questions on StackOverflow in the same vein as this; however, those questions deal predominantly (if not exclusively) with: other languages (PHP, Javascript, Ruby), Object (reference) type variables (Java, C++), or utilizing a variable's value to determine another's type (Java) - all of which I know how to do.

Here's What I Am After:

I have a variable:

String id = "variableIdentifier"

Is it possible, then, to initialize an int (it may be possible with Integer) with the identifier set to the value of String id?

The result I am looking for is int variableIdentifier where the identifier - again - is coming from the value of String id.

Some (Bad) Pseudo Code:

String id = "variableIdentifier";
Integer [needs name from id] = 0;

Solution

  • No, you cannot do that, you cannot name variables dynamically. Closest thing to that would be using HashMap. (Or some other Map implementation)

    hashMap.put(variableIdentifier,variableValue);  //put value
    hashMap.get(variableIdentifier); //get value