How to Create Variables at Runtime in Java

Is it possible to create variables at runtime in Java?

Is it possible to create variables at runtime in Java?

The simple answer is No.

Java is a static language and does not support the injection of new variable declarations into an existing compiled program. There are alternatives (in order of decreasing usefulness / increasing difficulty):

  • Represent your "variables" as name / value pairs in a Map. Or come up with some other design that doesn't require real dynamic variables.
  • Use a scripting language that runs on the JVM and is callable from Java.
  • Use some kind of templating mechanism to generate new source code containing the declarations, and compile and load it dynamically.
  • Use a byte code manipulation library (e.g. BCEL) to create class files on the fly and then dynamically load them.

The first approach is the best. Java is a static language, and works best if you don't fight it. If this is a problem for you, maybe you are using the wrong language.

The last two are difficult / complicated and have significant performance costs. They are almost certainly not going to help ...

How to create variables dynamically in Java?

A Map allows you to relate any key with any value. In this case, the key is the name of the variable, and the value is the value

Map<String, String> details = new HashMap<>();
for (int i = 1; i <101; i++) {
if (i<60) {
details.put("person" + i, "female");
}
else {
details.put("person" + i, "male");
}
}

Create variable at runtime

You can't create a variable name like that at all. If you only care about being able to generate a bunch of variables and don't care what the name actually is, then consider an array large enough for the most you think you'll want, or a Vector if you just don't know how many.

How to create new variable in java dynamically

No. Have you considered storing a Map<String, Object> in the class instead? The keys in the map would be the "variable names" and the values in the map would be the logical variable names.

If you could give more information about what you're trying to achieve (from a high-level perspective) that would help.

create variables dynamically, Data Types of java Variables

You can not create dynamic variables in Java because Java isn't a scripting language. YOu need to create variables in source code. But Java provides other ways to do this.
You can use arrays or Map<String, String> etc for this purpose.

Map<String, String> map= new HashMap<>(); 
int i=0;
while(true) {
// you can use whatever condition you need
details.put("key" + i, "val: "+i);
i++
// some condition to break the loop
}

How can I set the name of a variable at runtime in Java?

You can use a Map

String input1 = ...;
String input2 = ...;
Map m = new HashMap<String,String>();
m.put(input1, input2);

How to create a variable name at runtime?

Java is not a interpreted but rather a compiled language. So the compiler does not knows how to handle this. Such a thing might make sense in a scripting language.

If you need a custom Name for a "variable" maybe a construct like the following might make sense:

Map<String,Map<String,String>> varMap = new HashMap<String,Map<String,String>>();
varMap.put(objectName+" "+lineNumber, new HashMap<String, String>());


Related Topics



Leave a reply



Submit