Possible Duplicate:
Convert String to code
Run piece of code contained in a String
is there any way to turn a variable(string) into to a piece of code? My string has to be changeable (non-static) If you can explain it simply that would be nice :D
Possible Duplicate:
Convert String to code
Run piece of code contained in a String
is there any way to turn a variable(string) into to a piece of code? My string has to be changeable (non-static) If you can explain it simply that would be nice :D
I assume, that you want to create a String
object with some Java statements and execute the content as if it was a method.
No, it is not possible, because that would imply, that java was an interpreted programming language. Which is not the case.
You could merge the line with some sort of class template (on another string), store it to a file, compile that using the jdk (call javac with `Runtime.exec) and load the class with a custom classloader, then reflect the method and hope for the best.
There is a rather complicated way using the fairly new Java Compiler API, but I guess, that is far beyond your needs. (And there have to be some more complicated tricks, because the eclipse IDE provides views that allow executing Java statements.)