Assigning variables with dynamic names in Java

I would like to assign a set of variables in java as follows:

int n1,n2,n3; for(int i=1;i<4;i++) { n<i> = 5; } 

How can I achieve this in Java?

+68
java variables dynamic-variables
Jul 18 2018-11-11T00:
source share
7 answers

This is not how you do it in Java. There are no dynamic variables in Java. Java variables must be declared in source code (*). Period.

Depending on what you are trying to achieve, you should use an array, List or Map ; eg.

 int n[] = new int[3]; for (int i = 0; i < 3; i++) { n[i] = 5; } List<Integer> n = new ArrayList<Integer>(); for (int i = 1; i < 4; i++) { n.add(5); } Map<String, Integer> n = new HashMap<String, Integer>(); for (int i = 1; i < 4; i++) { n.put("n" + i, 5); } 

You can use reflection to dynamically access variables that have been declared in source code. However, this only works for variables that are members of a class (i.e., Static fields and instances). It does not work for local variables. See the @fyr quick and dirty example.

However, doing such things without the need for Java is a bad idea. This is inefficient, the code is more complex, and since you rely on runtime checking, it is more fragile.

And these are not "dynamic name variables". It better describes dynamic access to variables with static names.




* - This statement is a little inaccurate. If you use BCEL or ASM, you can "declare" variables in the bytecode file. But do not do it! So madness!

+84
Jul 18 '11 at 7:10
source share

If you want to access variables, some kind of dynamics, you can use reflection. However, Reflection does not work for local variables. It is applicable only to class attributes.

A rough quick and dirty example:

 public class T { public Integer n1; public Integer n2; public Integer n3; public void accessAttributes() throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { for (int i = 1; i < 4; i++) { T.class.getField("n" + i).set(this, 5); } } } 

You need to improve this code in various ways, this is just an example. It is also not considered good code.

+28
Jul 18 '11 at 7:30
source share

What you need is called array. I wanted to write the following code:

 int[] n = new int[4]; for(int i=1;i<4;i++) { n[i] = 5; } 
+11
Jul 18 '11 at 7:09
source share

Instead, use List or array

 List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); 

or

 int[] arr = new int[10]; arr[0]=1; arr[1]=2; 

Or even better

 Map<String, Integer> map = new HashMap<String, Integer>(); map.put("n1", 1); map.put("n2", 2); //conditionally get map.get("n1"); 
+10
Jul 18 '11 at 7:08
source share

Dynamic variable names in Java
There is no such thing.

In your case, you can use an array:

 int[] n = new int[3]; for() { n[i] = 5; } 

For more general (name, value) pairs (name, value) use Map<>

+4
Jul 18 '11 at 7:10
source share

Try as follows:

  HashMap<String, Integer> hashMap = new HashMap(); for (int i=1; i<=3; i++) { hashMap.put("n" + i, 5); } 
+3
Jul 18 '11 at 7:16
source share

No. The closest thing you can do is work with Maps to simulate it, or define your own objects that you need to deal with.

+3
Jul 18 '11 at 12:36
source share



All Articles