Using a String variable as an object name in a loop...Possible??
Joined: Mar 22, 2009
Hi is it possible to create objects when the object name is a variable? My code looks like this right now:
The program up to this gets a size between 3 and 10 and I want to create the size^2 number of boxes...so if the user enters 10 I want to create 100 box objects with names box00 - box 99. However I am currently getting the error message that :
strBoxName is already defined in createGrid(java.awt.Graphics)
any help at will be appreciated...thanks in advance!!
Joined: Mar 29, 2009
You can't to that
box strBoxName = new box(xPos,yPos,boxName,false);
This will create a variable named strBoxName, which will have nothing to do with the string strBoxName. So, when your loop gets to this line for the second time, you get the error since strBoxName is already defined by the first iteration of the loop.
May I suggest, use a 2D array of box objects and each time, create a new box at [verCounter][horCounter].