1

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

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Ethan Wass
  • 95
  • 1
  • 10
  • Possible duplicate of [Get variable by name from a String](http://stackoverflow.com/questions/13298823/get-variable-by-name-from-a-string) – Sudheesh Singanamalla Feb 07 '17 at 10:22
  • See if you can use a ``Map`` instead. – f1sh Feb 07 '17 at 10:22
  • Possible duplicate of [Creating a variable name using a String value](http://stackoverflow.com/questions/8631935/creating-a-variable-name-using-a-string-value) – CloudPotato Feb 07 '17 at 10:23
  • 2
    This is most certainly *not* what you want to do. There are better ways to do this. Reflection *can* do this, but most of the times, you don't. – Steffen Feb 07 '17 at 10:24
  • Also note that there is a widely accepted convention in java that class names should start with an upper-case letter, while variable names should start with a lower-case letter. – Hulk Feb 07 '17 at 11:19

1 Answers1

5

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")
rkosegi
  • 14,165
  • 5
  • 50
  • 83