This discussion is archived
3 Replies Latest reply: Dec 4, 2012 8:32 AM by rp0428 RSS

How to create Variable dynamically with a String value ?

Ajay Sharma Newbie
Currently Being Moderated
Hi,

I want to create variable dynamically in java.

If user enter "India" then want to create variable
String India;
Is it possible ? If yes then How ? If no then what is the reason behind this ?


Regards,
Ajay Sharma
  • 1. Re: How to create Variable dynamically with a String value ?
    Kayaman Guru
    Currently Being Moderated
    Ajay Sharma wrote:
    I want to create variable dynamically in java.
    Well you can't. Java isn't a scripting language.
  • 2. Re: How to create Variable dynamically with a String value ?
    gimbal2 Guru
    Currently Being Moderated
    No, Java is a compiled language, not an interpreted language.

    However you can easily define a Map (HashMap for example) and use that for your purposes.
  • 3. Re: How to create Variable dynamically with a String value ?
    rp0428 Guru
    Currently Being Moderated
    >
    I want to create variable dynamically in java.

    If user enter "India" then want to create variable

    String India;

    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.
    http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html

    And here is a link with examples of the EVAL function
    http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript
    JavaScript there are 2 ways by which you can create dynamic variables:
    1) eval Function
    2) window object
    
    EVAL Function:
    It is said that eval is one of the most powerful function inside javascript. We can execute javascript code, read JSON Object using eval function. But here we will learn how to create dynamic variable using eval function.
    
         var data = "testVariable";
         eval("var temp_" + data + "=123;");
         alert(temp_testVariable); 
    
    Here i have created an dynamic variable temp_testVariable holding value 123. You can also create arrays using eval function.
    
         var data = "testVariable";
         eval("var temp_" + data + "= new Array();");
         temp_testVariable[temp_testVariable.length]="Hello World";
         alert("Array Length: " + temp_testVariable.length);
         alert("Array Data: " + temp_testVariable[0]); 
    And a couple more links with other answers to your original question
    http://stackoverflow.com/questions/1192534/is-there-away-to-generate-variables-names-dynamically-in-java
    http://stackoverflow.com/questions/7478833/is-it-possible-to-create-variables-at-runtime-in-java

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points