This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Equivalent of eval() function in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equivalent of eval() function in java" Watch "Equivalent of eval() function in java" New topic

Equivalent of eval() function in java

Pradyumna Hari

Joined: May 27, 2002
Posts: 15
Hi all,
Can any one tell me what is the equivalent of
eval function ( this is a function in javascript ) in java. The requirement is to evaluate the value of an expression but the expression is itself a string.
ClassA a = new ClassA();
String strExpression = "a.getName()" ;

Now I want to print the value returned by a.getName(). Is there any function ftn such that
System.out.println(ftn(strExpression)) would print the value returned by a.getName()

[ June 04, 2002: Message edited by: Pradyumna Hari ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

You're looking for reflection.
You can get a Class from an instance with

then you can have a look at the class and do things like find and call methods etc.

or something like that...
I didn't actually test the code, but it should get you started.
I agree. Here's the link:
subject: Equivalent of eval() function in java
jQuery in Action, 3rd edition