aspose file tools*
The moose likes Beginning Java and the fly likes How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc 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 "How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc" Watch "How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc" New topic
Author

How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc

AJ Taylor
Greenhorn

Joined: Feb 02, 2007
Posts: 2
Hi guys,

I've just started learning java and I'm trying to work out how i can create x n number of the same object (myObject) and assign them to a reference variable which the name increments by one each time, so i create myObject1, myObject2, myObject3 etc.

Here is the code. The issue is line #4 - i cannot use the String name i created on line 3, i'm stuck so help / suggestions are greatly appreciated.

Thanks,

AJ

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Will string arrays be useful here?

ie.

String myRefVar[] = new String[10];

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Or more generally, arrays of any kind.

myObject[] objects = new myObject[10];

...

objects[i] = new myObject();


[Jess in Action][AskingGoodQuestions]
AJ Taylor
Greenhorn

Joined: Feb 02, 2007
Posts: 2
Henry Wong wrote:Will string arrays be useful here?

ie.

String myRefVar[] = new String[10];

Henry



Yes it would! Thanks guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create reference variables in a loop with incremental names - myRefVar1, myRefVar2, etc
 
Similar Threads
Objects and Instance Variables Confusion...
MultiDimenaional Array sort
set Object direct or indirect?
Spring MVC 3 and JSF2 Integration
Multiple clients in RMI