Ed said:
Steve said:
And what name would you like to find the Widget has after this bit of
code:
Widget w = new Widget();
Widget x = w;
Objecy y = x;
w = null;
// I suppose its name at the moment is "x and y".
Or this bit of code:
Widget[] widgets = new Widget[5];
Widgets[0] = new Widget();
// now it doesn't have a name at all.
Perhaps you are looking for something like a java.util.Map, where you
can store a lookup map of Object->Object. Using Strings for the key is
common.
Everything has a name. That's how you reference it in code, so, the name
of your Widget is "widgets," and the name of the first widget is
"widgets[0]."
And these names are just aliases for the address space they point to.
Ed.
How about this:
List l = new LinkedList();
l.add(new HashMap()); // and the name of the HashMap is???
((HashMap) l.get(0)).put("Gizmo", new Widget());
So now what's the name of the Widget instance?
I really don't think it makes sense to ask what the "name" of the
object is. That name will vary vrom object to object. It's immediate
name (as known by the HashMap code) cannot be known without examining
the source code for a HashMap, and may vary depending on the VM
library version / maker being used at the time.
Different pieces of code (even within the same class) have different
names for objects. A chain of objects pointing to other objects pointing
to other objects will have a chain of names, possibly involving index
numbers. Many such chains may end up pointing at the same object.
It really makes no sense to think that an object has "a name".
Steve