1

I am creating objects of the class Room, but I dont want them in a list/ array. The standard method would be

Room room1 = new Room();
Room room2 = new Room();
Room room3 = new Room();
Room room4 = new Room();

etc..

since the names of the rooms matter I was thinking I could do:

for(int i=0; i<(whatever);i++){
Room (room + i) = new Room();
}

this dont work. Does anyone know if I can do this easily as I need to name them room1, room2 and so on.

mynameishi
  • 59
  • 7

1 Answers1

0

No. Simply you cannot. Variables names should be statically typed (at least without any byte code manipulations).

If you want to access them with your index (i) later, use array or ArrayList.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307