• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Naming Objects

 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a loop, and each iteration of the loop should create a new instance of an object, how would you tell Java to name the objects sequentially (or differently, for that matter)?

loop does this:
Customer c1 = new Customer();
Customer c2 = new Customer();
Customer c3 = new Customer();

Each iteration of the loop, how would you tell it to name them different?




This isn't actual code, just something to illustrate the question.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use an array or ArrayList.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could store them in an array:

Then access them the usual way for arrays:


You could store them in a list:

Then access them the usual way for lists:
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thing is, the program will either need to create a new customer, or not, depending on events described in an input file. If it needs to create a customer, it will either place the customer in a "Work Bay", or if the Bay is full, it will drop it in a queue. I could put it in element 0 of an ArrayList, then place that element into the Bay or enqueue it in the queue, but I'll need to reference it later. What would the name of the object be?

What about this:

[ March 03, 2006: Message edited by: Greg Roberts ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to be able to retrieve a particular object based on an attribute or name, then you could either use an instance variable to hold the name or store the object in a HashMap.
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects don't have names, only variables do. So if you create a customer:and add it to an ArrayList:you can subsequently retrieve it from that list and assign it to a different variable:There's only one Customer object here. First you had a variable named "fred" that contained a reference to it. Next the ArrayList got given a reference to that same object. Finally you copied the reference out of the ArrayList and assigned it to the variable named "annanicole". All one object, several references to it.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Lynn:
If you need to be able to retrieve a particular object based on an attribute or name, then you could either use an instance variable to hold the name or store the object in a HashMap.


I don't need to retrieve them later, but I do need them to be unique object references. I'm using a queue to hole a "line" of customers, and a "bay" where the customers go when it is their turn in line. Will this work, not giving the customer references unique names? They are unique in that they each have their own customer number, and I'm passing the customer into the queue or into the bay.
 
Amy Medrat
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try HashMap

HashMap customerQue = new HashMap();
for( int i = 0; i < 5; i++ )
{
customerQue.put(""+i,new Customer());
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Roberts:
Will this work, not giving the customer references unique names?


Yes. This is one of those small hurdles of understanding that every good programmer has to go though: an object is distinct from any "names" (variables) that refer to it. An object can have any number of "names", including zero, just as you personally might have any number of mentions in today's Washington Post. The number of times you're mentioned in the paper has no effect on you personally; you continue to exist regardless of what that number is.''

Even if an object has no "name", it can still be contained in arrays, Lists, Maps, or other data structures, so it's still quite usable. One "name" can refer to a succession of objects, and one object might gain and lose any number of "names" during its lifetime.

Now, of course, if no one knows you exist, then you can be garbage collected
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The number of times you're mentioned in the paper has no effect on you personally; you continue to exist regardless of what that number is.


Unless you are mentioned in the obituaries. Seriously, very nice analogy.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object can have an "identity", though. For example you might not want to make a new object for customer 123 if you already have one in queue. A Map is a nice way to keep track of objects by some "key" or identity. If you don't need that kind of thing, let's not confuse the conversation too much. But if you do, read on up Map and see if that would help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic