1

As an exercise (note that I am a programming student, but this is an exercise to satisfy my own curiosity, not for a graded assignment) in using instance variables, I would like to create an unspecified number of instances of a class and assign a unique name to each. My idea would be to make a loop such as:

int i=1;
while (! response.equals("quit")){
    SomeClass object_i = new SomeClass();
    i++;
...
}

This would allow the program to create as many instances of SomeClass as needed by the user, then be able to refer back to instance variables in each one such as:

for (i=1; i <= count; i++){
    sum += object_i.nonStaticInstanceVariable;
}

There is a suggestion for unique identifier names using AtomicLong at this thread: java for loop executes too fast gives System.currentTimeMillis() duplicate

but I am unsure of how I would refer to instance variables in particular objects created using this method. Can anybody suggest a way to do what I'm trying to do in Java?

Many thanks.

Community
  • 1
  • 1
wayeast
  • 11
  • 1

5 Answers5

1

In your first for-loop, you're creating instances of SomeClass, but you're not doing anything with them. What you want to do is gather them up in some kind of collection, so you can refer to them later, like this:

int i=1;
List<SomeClass> classes = new ArrayList<SomeClass>();
while (! response.equals("quit")){
  classes.Add(new SomeClass());
  i++;
  ...
}

Now you can loop through your collection and do something useful with them:

for (SomeClass someClass : classes) {
  sum += someClass.nonStaticInstanceVariable;
}
matt
  • 9,113
  • 3
  • 44
  • 46
  • I don't think you shouldn't spoon feed that much. This is a very elementary problem that should be learned through documentation, not copy and pastable code. – user606723 Jul 14 '11 at 15:50
0

I would use a map and a universally unique identifier (UUID). See java.util.UUID for an example.

Map<UUID,SomeClass> map = new HashMap<UUID,SomeClass>();

for (int i=0;i<1000000;i++) {
    UUID key = UUID.randomUUID()
    map.put(key, new SomeClass());
}

Now if the user has a UUID then they can get a reference to the appropriate SomeClass

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
0

lol, you must really just be starting.
Read this- http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Most of what you have makes sense, but you need to do-

SomeClass myObject[i] = new SomeClass();

EDIT: Other people's options will work too, but you should learn to do it this way first.

user606723
  • 4,918
  • 2
  • 27
  • 34
  • Although the array may not meet his requirements, as he will be forced to define the upper limit for the array... Although, in all honesty, I believe every student should learn how to write their own ArrayList type of class (just so they can appreciate it, not so they can use it!). – Clockwork-Muse Jul 14 '11 at 16:39
0

You can't create instance variables at runtime (short of compiling and loading a new class)

Use an array or a Map for this

(Or use a dynamic language like Groovy that runs on the JVM, which internally will use a map-like structure)

antlersoft
  • 14,636
  • 4
  • 35
  • 55
0

I think you would just want to use some sort of collection such as an array or arraylist.

List list = new ArrayList();
        int i=1; 
       while (! response.equals("quit"))
    {     list.add(new SomeClass());     
    i++;
} 

It could then be accessed by getting index in the ArrayList

list.get(index);
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189