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

5
  • Possible duplicate of Get variable by name from a String Commented Feb 7, 2017 at 10:22
  • See if you can use a Map instead. Commented Feb 7, 2017 at 10:22
  • Possible duplicate of Creating a variable name using a String value Commented Feb 7, 2017 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. Commented Feb 7, 2017 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. Commented Feb 7, 2017 at 11:19

1 Answer 1

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") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.