Calling a variable by name that is not known at compilation

Is it possible to set a variable if I want it to be flexible? I think exmaple makes understanding easier.

String hallo1; String hallo2; for(int i = 0; i < 2; i++) { hallo & i = Integer.toString(i); } 
+4
source share
5 answers

If you really need to have variables with names unknown at compile time, you can achieve this effect by creating a structure that contains arbitrary names and values. For example, you can create a HashMap where the key is the name. Then your example above looks something like this:

  HashMap myData=new HashMap(); for (int i=0;i<2;++i) { myData.put("hallo"+i,Integer.toString(i)); } 

Later you pull them out:

  String whatever=myData.get("hallo1"); 

and etc.

Of course, you cannot directly access the values ​​from the map as variables, you will always need to make and receive updates and extract them, but the concept is the same.

However, I would be very careful about this because it makes code maintenance difficult. If the required names really get out of hand - if you write common code to read an arbitrary database table whose name was entered by the user at runtime or something like that - cool. But if you think this is a convenient shortcut for something like:

  if (region.equals("1")) region1Total+=amount; else region2Total+=amount; 

My simple answer would be NOT! Code is much easier to maintain if you use the IF statement and regular variables. Then anyone who reads the code can look at your ads and see what all the possible variables are. You can search for text to find wherever they are used. If you name the variable incorrectly, instead of magically creating a new variable, you will receive a clean compilation error message. Etc.

On the other hand, if you CAN write something like

  String n=getInputFromScreen(); String s=getAnotherInputFromScreen(); hallo & n = s; 

you would not have imagined which variables exist in your program and there is no way to track where they are used.

+2
source

No, you can’t. There (fortunately) there is no such thing as eval() in Java.

It is best to take an Array or ArrayList . Here is an example of an array:

 String[] hallos = new String[2]; for (int i = 0; i < 2; i++) { hallos[i] = Integer.toString(i); } 
+13
source

This is technically possible with a reflection , but I would advise it if you do not have really good reasons for this. This will make your code much harder to debug and read.

EDIT:
The comments emphasized:

  • To clarify, I highly recommend not being tempted to use reflection. Other answers point to better ways to achieve your goal, such as an array.
  • Reflection will not help you with local variables.
+4
source

No, a way to do something like an array, a list, or something similar. It is also more intuitive to group them together to have many variables (which really need to be connected) floating around. It is impossible to do this - at least in Java. I saw this in scripting languages, but this is completely different ...

0
source

You can use an array of strings.
Sort of:

  String[] hallos = new String[10]; //when looping you can use regular (counter) loop or for each loop for (String s: hallos){ //do whatever you want with each String } 
0
source

Source: https://habr.com/ru/post/1300746/


All Articles