• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object identifier

 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having a slight problem in my JSP project. Basically, my project is a two-player game over the Internet. There are two bean classes - Player and Game. Now, I want these two players to use the same Game object. The problem is: the name of the Game object cannot be hard-coded into the JSP code. So, for example, there will be a auto-generated name for the Game object such as game123, and I want the JSP document to create an instance of Game by:

Game game123 = new Game();

However, I cannot hard-code it this way, because the next game to be shared by two players will be called game124.

Intuitively, I thought the answer would be to store the name of the Game object in a String and then use it to create the object. But does anyone know if Java allows objects to get their names from String variables?

If that is not possible, has anyone faced such a problem before and know a way to get around this? I am open to any sort of idea, because I really need a way to get around this.
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why dont you put the game object in a HashMap with the name of the object as a key?

BTW, why are you doing this? Do you want a differrent game object per user that you want to persist across requests? You could easily store the game object in the session
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Tobie H",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jayesh,

That's what I was looking for! The HashMap thing works perfectly for me. Thanks!

I have to do this because the same Game object is used by two different users. If I store it as a session, I cannot have both the players accessing it.

Originally posted by Jayesh Lalwani:
Why dont you put the game object in a HashMap with the name of the object as a key?

BTW, why are you doing this? Do you want a differrent game object per user that you want to persist across requests? You could easily store the game object in the session
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tobie Henderson:
Hey Jayesh,


I have to do this because the same Game object is used by two different users. If I store it as a session, I cannot have both the players accessing it.





gotcha!! but, wont all your game objects build up in your system over time?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic