Hey there! Does anyone know how to "generate java code" during runtime? Like this: String exerciseNr = "3_1_2"; and then I want to run the following: ("exerciseDialog" + exerciseNr).setVisivle(true); Should run this: excerciseDialog3_1_2.setVisivle(true); Hope you understad my question... Greetz from Praiser
Here's what you needs to do: 1) Generate the code and write it out to a file. 2) Using Runtime.exec() invoke the compiler (javac) and compile the Java source code into a class file. 3) Using a class loader (I think you'll have to write your own, although it's not that hard), you then need to explicitly load the class during runtime. 4) Once the class is loaded you can access methods and fields of the class, as well as create instances. Chances are, this is easier said than done. Are you sure you need to do this? --Mark
In your example, you're building the name of a class. Does the class already exist? If so, you can use:
Note that you had to know the type of the class (SomeType) to do this. Since you apparently have a bunch of classes with different exerciseNr we don't know the which one we'll get at coding time. How to resolve this? The SomeType we put here is a superclass or interface that all the exercises extend or implement. Methods we call like setVisible() must be defined on SomeType and can be overridden by individual exercises. This technique requires the class to have a no-argument constructor, usually not a big deal. If you want to use more complex constructors you'll have to dig deeper into reflection.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I don't think he's building the name of a class, but the name of a variable. He's got more than one variable named exerciseDialog3_1_1, exerciseDialog3_1_2, etc, and he wants to make one of them visible, but which one he's not sure until runtime. Mark's answer is correct if you really need to generate code, but if all you need to do is choose which variable to operate on, then selecting among hardcoded variable names by generating code is a crazy way to do it! Instead, why not put all your exerciseDialogs into a Collection (for example, a java.util.HashMap) and then look them up by name as needed:
[ October 21, 2003: Message edited by: Ernest Friedman-Hill ]
Ernest is right here. You are making a simple thing complicated. I'd recommend his approach, or perhaps use reflection if you are operating on fields. (I hope you aren't talking about local variables, because local variable names are lost at compile time and don't make it into the class file or the java runtime - aside from debugging info) You definitely don't want code generation. If you are just deadly curious how to do dynamic code generation, I wrote an article on the topic for Java Developers Journal a couple years ago: http://www.sys-con.com/java/article.cfm?id=1307 You don't want to do anything so complicated for this, but I thought I'd throw it out just in case someone out there has an interest in the topic.
author and iconoclast
It was a fun article to write. It is a bit dated now, but it is still interesting. If you liked that one, you might also enjoy my "Hello World! in 70 bytes" article. It's kind of an introduction to class files and bytecode from the completely irrelevant, but highly enjoyable, perspective of trying to produce the smallest Java "Hello World!" program... http://www.sys-con.com/java/article.cfm?id=1533