aspose file tools*
The moose likes Beginning Java and the fly likes Using a String variable as an object name in a loop...Possible?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a String variable as an object name in a loop...Possible??" Watch "Using a String variable as an object name in a loop...Possible??" New topic
Author

Using a String variable as an object name in a loop...Possible??

Marc Holbach
Greenhorn

Joined: Mar 22, 2009
Posts: 2
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!!
Alexis Michael
Greenhorn

Joined: Mar 29, 2009
Posts: 9
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].

cheers
Marc Holbach
Greenhorn

Joined: Mar 22, 2009
Posts: 2
Thanks a lot gonna try that right now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using a String variable as an object name in a loop...Possible??
 
Similar Threads
XML Parsing Issue
obtaining drawing coordinants from an array
Using XPath to read list of nodes
2D Array removing Duplicates
Moving multiple ojects in Java2D