Forums Register Login

Equivalent of eval() function in java

+Pie Number of slices to send: Send
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.
Example
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()
Thanks

[ June 04, 2002: Message edited by: Pradyumna Hari ]
+Pie Number of slices to send: Send
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.
Dave
I don't always make ads but when I do they're tiny
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 11967 times.
Similar Threads
calling a method in jsp
Why to use eval function inside Javascript
Eval in java
Undefined on jQuery Ajax Request
calculate a string expression
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:20:13.