This content has been marked as final. Show 3 replies
I want to create variable dynamically in java.
If user enter "India" then want to create variable
Is it possible ? If yes then How ? If no then what is the reason behind this ?
No - if you mean can you add a dynamically named variable at run-time to your pre-compile class. The reason is that Java is a statically typed language.
But you can get the use of dynamically named variables and the equivalent of what you are asking by using the Java Scripting API which has an 'eval' function. So it really depends on what you mean by 'possible' and what you mean by 'in Java'.
If you search for 'java create variables dynamically script' in your search engine you will see that this question has been asked and answered many times.
Here is the link for the Java Scripting Programmer's Guide with examples.
And here is a link with examples of the EVAL function
And a couple more links with other answers to your original question