File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Naming Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Naming Objects" Watch "Naming Objects" New topic
Author

Naming Objects

Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
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.


Greg Roberts<br />CIS Student<br />University of West Florida
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I would use an array or ArrayList.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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:


There is no emoticon for what I am feeling!
Greg Roberts
Ranch Hand

Joined: Feb 05, 2005
Posts: 72
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

Joined: Feb 07, 2005
Posts: 2367
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Feb 05, 2005
Posts: 72
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

Joined: Mar 05, 2006
Posts: 15
You can try HashMap

HashMap customerQue = new HashMap();
for( int i = 0; i < 5; i++ )
{
customerQue.put(""+i,new Customer());
}


medRat
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.
 
 
subject: Naming Objects