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 ]
Joined: Feb 07, 2005
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.
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.
Joined: Feb 05, 2005
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.
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
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.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 29, 2003
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.