jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes How can I instantaite 10 objects at the same time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I instantaite 10 objects at the same time?" Watch "How can I instantaite 10 objects at the same time?" New topic
Author

How can I instantaite 10 objects at the same time?

drew taylor
Greenhorn

Joined: Feb 20, 2006
Posts: 21
I have a Student class that sets up random student attributes, and I have a ClassRoom class that contains an ArrayList to store student reference variables. Given that a set of 10 students will need to be created often and assigned to a classRoom, how can Instantaite 10 different student objects at the same time?

I tried making a while loop to loop 10 times but it only created the same object over and over and added it on top of the first one in the ArrayList in the ClassRoom class.

So how can I make 10 different objects without 10 separate object declarations that, when called will always replace the previously instatiated objects with the same reference variable names?
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
Go ahead and post your code, use [code] tags around it for readability.

There's no "built in" functionality to create random objects for you. You will have to either instantiate them as you have said, or write a service method to create "random" objects for you.
drew taylor
Greenhorn

Joined: Feb 20, 2006
Posts: 21
Ok, here is my code in the main class where I intstantiate a new student object: ( I have included the while loop I tried to make 10 objects and add them to an ArrayList in the ClassRoom class which doesn't work)


The other code I have are the constructors in the ClassRoom class and Student class.

What does making a service to create random objects involve?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by drew taylor:
What does making a service to create random objects involve?
One important feature of such a service is that it creates objects. That sounds obvious, but your code doesn't create any new objects inside that while-loop. It simply adds the same Student object four times to the Tutor object.

Code that creates four new Student objects and adds them to a Tutor would look more like this:
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Note that the more idiomatic way to code the loop would be using a for-loop in place of the while-loop.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
I have a Student class that sets up random student attributes


Post your Student class. From this code, you are trying to add the SAME student to the class several times.

The code might look something like:



Then you'd have to write a static method in the Student class to "randomly" generate the attributes of a student.

It's fairly trivial to do this, I'll give you some pointers after you post your Student code.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Hi Drew,

Have a good, line-by-line look at what your code does.

Student student = new Student();
Here you create a Student object.

Tutor tutor = new Tutor();
And here you create a Tutor object.

Here you have a loop where you add the same Student object to the Tutor object four times. You are not creating multiple Student objects; you are just calling addStudent(...) on the Tutor object four times with the same Student object.

You should put the "new Student()" line inside the loop, so that a new Student object is created at every iteration of the loop.

You'll also have to set the properties of the student to random values.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
GeeCON Prague 2014
 
subject: How can I instantaite 10 objects at the same time?