3

I was wondering if in Java there was any way to give a new variable a name which is the value of another variable. The below code is an non working example of what I'm trying to do.

int a = 0;
while(true){
    String (a) = "newValue";
    a = a + 1;
}

or

String b = "newVariableName";
int (b) = 1;

Thank for any help given.

3
  • 1
    what you are trying to achieve? Why do you need such functionality? Commented Feb 5, 2012 at 19:54
  • Almost guaranteed that you don't actually need or want that. It's an XY problem. Edit. You should also take to heart the Zero-One-Many rule of programming. Commented Feb 5, 2012 at 19:55
  • Java doesn't work that way, and not only that a variable name is a lot less important than you think it is. In fact in compiled code, the name almost doesn't exist. If you want to associate an object with a String, use a Map, or an object with a contiguous number, use an array. Commented Feb 5, 2012 at 19:56

6 Answers 6

4

No this is not possible. It is possible to create a new object and add it to an array or some sort of list. Then you can reference the new object by the subscript. So a rough example could be:

Object[] a = new Object[30]; 

for(int i = 0; i < 30; i++)
{
a[i] = new Object();
}

and then you could use

a[some number].someFunction();

If you think about it, how can the compiler know what the value of a certain true variable is going to be, outside of the actual program run time? It is unlikely that you need to actually do what you want however.

Sign up to request clarification or add additional context in comments.

Comments

1

You're trying to make an array or a list or a dictionary.

2 Comments

That isn't really an answer. On the face of it, the OP is asking for some sort of reflection.
@KerrekSB: This isn't what he's asking for; it's what he actually needs.
0

You cannot create dynamic variable names in Java. I would recommend placing these variables in a Map with a string as a key. The key can be whatever String you would like.

1 Comment

Sorry for getting back late. I found another way to get the job done but after a lot of reading that, no, dynamic variables can't be used in Java. I had done it with PHP before and was looking for a similar method. Though Mapping the data using methods like HashMaps would work. As suggested by others. Thanks for the help guys.
0

You can't do this. Use a Dictionary or a Map to store the values.

Comments

0

No, there's no way to do such a thing in Java. The closest you could do would be to create code at runtime and load a new class with fields made with dynamic names.

You might want to consider something simpler like a HashMap instance.

Comments

0

I dont think reflection will help because there is no way how to create new instance of Field. So something like

Map<String,Field> 

wont work. Use javascript instead :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.