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.
What about this:
[ March 03, 2006: Message edited by: Greg Roberts ]
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