How to convert a string into an objects name?
Hi
I have the following problem;
I have a string
String a = "e1"
and a method
call(Event e1) that calls the "Event" class with object the name of the string a.
How can I convert the strings name into an object so as to use it in the "call" method?
Any ideas?
Thanks in advance
[332 byte] By [
natenia] at [2007-11-27 10:22:32]

No, you cannot dynaically name variables in java. The closest you can get to doing what is seems you want to is to use a map to store objects in, using the strings as a key
void respond(){
Event evt = (Event)eventMap.get("e1");
call(evt);
}
~Tim
I don't know if this helps you, but you can do things like this.
String a = "com.example.SomeEvent";
//Instantiate com.example.SomeEvent
Object o = Class.forName(a).newInstance();
SomeEvent must have a non-argument constructor for this to work.
Class that will be instantiated depens on runtime value of String a, and can be different across different executions of the program.