aspose file tools*
The moose likes Beginning Java and the fly likes Creating Multiple Objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating Multiple Objects?" Watch "Creating Multiple Objects?" New topic
Author

Creating Multiple Objects?

Dante Hawke
Greenhorn

Joined: Jan 27, 2009
Posts: 7
Hey all,

I need to make 100 'Customer' Objects that are each assigned a randomly decided on string as their parameter. - I can do this via a random number generator with each number referring to a different string.

e.g.

Possible strings: "Hello" or "Goodbye" or "Farewell".

Customer C1 = new Customer("Hello"); x 100

How can I guarantee that the reference variable to each new object will be different? As after I create 100 of them, I must call a method on each of them 100 times.

Any help is most appreciated,

Many Thanks.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you need to keep a reference to each object then you'll want to use an array or some sort of collection.
Dante Hawke
Greenhorn

Joined: Jan 27, 2009
Posts: 7
Ah of course how silly of me. I now create an array and create each object with a reference variable and place that within each element. Does it matter that the reference variable is the same still though?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
You want an array which contains "Hello" "Goodbye" and "Farewell" and use the random number as the index to use those numbers in the Customer constructor.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

i would also factor out the common code, and combine the three 'if''s into a single if-else. Note that if i ==1, it cannot == 2 or ==3. you'd be better off with

if (ranNum == 1) {}
else if (ranNum == 2) {}
else if (ranNum == 3) {}

further, it's redundant to have

customers[i] = Ci;
customers[i].queueUp(Q1);

three times. I'd factor it out to outside of your if-else entirely.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

fred rosenberger wrote:Note that if i ==1, it cannot == 2 or ==3. you'd be better off with

if (ranNum == 1) {}
else if (ranNum == 2) {}
else if (ranNum == 3) {}

Or a switch statement:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

Good point...I, personally, don't like switch statements, but have no good reason for my dislike. I can't say there is anything wrong with them...they're just not my style.
Dante Hawke
Greenhorn

Joined: Jan 27, 2009
Posts: 7
Thanks for the input guys, my final 'working' code is:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
That looks a lot better. Well done.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
But you would do well to get your spacing consistent. You have some binary operators with spaces and some without. That will lose you marks. Recommend: a binary operator is preceded and followed by single spaces. That includes =.
 
Don't get me started about those stupid light bulbs.
 
subject: Creating Multiple Objects?