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]
# 1

Say what....? o_0

MeTitus

Me_Titusa at 2007-7-28 17:16:38 > top of Java-index,Java Essentials,Java Programming...
# 2

ok...

To help I use extra code

void call(Event e){

...

}

void respond(){

String a ="e1";

.......

call(e1);

}

Is this more clear?

I want to give the name of the object dynamically that is the name of the object will be the content of the string "a".

natenia at 2007-7-28 17:16:38 > top of Java-index,Java Essentials,Java Programming...
# 3

You can't, forget about that. You can't replace things like that in the code.

-Kayaman-a at 2007-7-28 17:16:38 > top of Java-index,Java Essentials,Java Programming...
# 4

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

SomeoneElsea at 2007-7-28 17:16:38 > top of Java-index,Java Essentials,Java Programming...
# 5

thanks

natenia at 2007-7-28 17:16:38 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

boruthadzialica at 2007-7-28 17:16:38 > top of Java-index,Java Essentials,Java Programming...