Ethan Wass
Ethan Wass

Reputation: 105

Java: how to access a class by a name saved in a variable?

I have something like this:

String currentRoom = "NameOfRoom";
static room NameOfRoom = new room();

And I want to activate a function that is inside the room by accessing it through the variable currentRoom.

Is it possible?

Thanks in advance, Ethan

Upvotes: 1

Views: 83

Answers (1)

rkosegi
rkosegi

Reputation: 14618

Not sure if I got you.

But you can use Map to maintain mapping between strings and object instances.

Map<String, Object> map = new HashMap<>();
map.put("NameOfRoom", new Room());

Now retrieve from map

map.get("NameOfRoom")

Upvotes: 5

Related Questions