Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic evaluation

Posted on 2006-07-07
4
Medium Priority
?
775 Views
Last Modified: 2008-02-20
Java experts,

Is it possible to do dynamic evaluation in Java?

public class test
{
    public static String username;    

    public static void set(String name, String value)
    {
        eval(name + '=' + value);
    }

}



If I called:
      test.set("username","some_name");

the value "some_name" would get placed in the username String.
0
Comment
Question by:a1programmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17058248
You could use a Java app-enabled scripting language (try Groovy) or beanshell
0
 
LVL 12

Accepted Solution

by:
enachemc earned 1000 total points
ID: 17058327
You can do that through reflection (the java.lang.Reflect package). You can dinamicaly obtain a reference to a field or method. You can assign to the field references or invoke the methods. If you want to set field values (you can use the setters) to instances of some, you would first require the instance.

Example of setting an instance's field value:

public static void set(Object instance, String name, String value)
    {
       Field f = instance.getClass().getField(name);
       //the type of your field should be String (if it's another type, you should convert from String to the required type)
       f.set(instance, value);
    }

0
 
LVL 7

Author Comment

by:a1programmer
ID: 17058621
That's exactly what I was looking for.  

Thanks!
0
 
LVL 7

Author Comment

by:a1programmer
ID: 17058626
CEHJ,
   Thank you as well for recommending those scripting language.   I had never heard of either, but will keep them at hand for the future.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question