1

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;
Thomas
  • 6,291
  • 6
  • 40
  • 69
  • Your question is not very clear. Could you share code? Possible solution is in http://docs.oracle.com/javase/tutorial/reflect/ – Jayan Feb 08 '15 at 12:52
  • 1
    Actually there is a topic with the same question , take a look here - http://stackoverflow.com/questions/8631935/creating-a-variable-name-using-a-string-value – vtor Feb 08 '15 at 13:03

1 Answers1

4

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
FINDarkside
  • 2,102
  • 1
  • 18
  • 26
  • 1
    Thanks. I'm going to hold off on accepting your answer for a few minutes. You are confirming what I think, too. Just want to be absolutely 100% before saying "yes". Edit: just tried to accept -- SO requires a wait. – Thomas Feb 08 '15 at 13:01