Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create variables at runtime in Java?

For example, say I wanted to "extract" String[] fruits = {"Pear", "Banana", "Apple"}; into three separate variables, eg:

for (int i=0; i != fruits.length; ++i) {
    // of course there's no eval in Java
    eval("String fruit + i = " + fruits[i] + ";"); 
}

// ie: code that creates something equivalent to the following declarations:
String fruit0 = "Pear";
String fruit1 = "Banana";
String fruit2 = "Apple";

How could I do that, ignoring the "Why the heck would you want to do that?" question that you might be urged to ask me.

Similar questions have been asked many times before, but the real answer was never given, because what the OP really needed was to use a different approach. That's fine, but is this possible at all?

I have looked at reflection and it doesn't seem like there are any methods that would allow me even to add extra fields to an instance, let alone dynamically create locals.

like image 806
NullUserException Avatar asked Sep 20 '11 00:09

NullUserException


People also ask

What is a runtime variable?

With runtime variables, you can define values that can be used as source values in Outbound Mappings or Conversation Variable Mappings in the same rule. This way the actual mappings can be made simpler, more readable and you can avoid repeating the same kind of configuration many times.

Is object created at runtime in Java?

Objects in Java are only created at Runtime. Objects are created runtime by Java Virtual Machine based on classes that you create as a programmer.

What are the rules for creating variables in Java?

Rules to Declare a Variable A variable name can consist of Capital letters A-Z, lowercase letters a-z digits 0-9, and two special characters such as _ underscore and $ dollar sign. The first character must not be a digit. Blank spaces cannot be used in variable names. Java keywords cannot be used as variable names.


3 Answers

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 ...

like image 111
Stephen C Avatar answered Sep 20 '22 10:09

Stephen C


The question is not why you want to do it but 'what are you going to do with it?'. So suppose at runtime variable with the name fruits2 magically appeared on the stack of your method. Now what? You had to know its name at compile time to take advantage of it. Reflection will not help you access local variables.

Anyway, I would be interested if you described more detailed use case.

like image 26
Alex Gitelman Avatar answered Sep 21 '22 10:09

Alex Gitelman


The way you phrased your question, people won't understand what you're asking. I believe (if I DO understand) the answer to your question (which should be phrased: "is it possible to dynamically create variables at run time") is "not as you've presented it".

You're right, there's no analog to javascript's (very powerful, but slow and fraught with hazards "eval" function) in Java, and that is precisely what you would need to get this to do what you're hoping to do.

The closest that exists is a hashmap (which is actually pretty close) where you can designate the key at run time, and then set the value. It's fairly versatile as you can have an map that will allow for whatever type you want stored in the field.

like image 40
Yevgeny Simkin Avatar answered Sep 19 '22 10:09

Yevgeny Simkin