I asked a similar question and was able to work it out and get some answers,
but this one is a bit harder. If I do this:
MyObject myObj = new MyObject();
Is there any way I can, from within myObj, get the *name* of that particular
object? I know I can do this:
myObj.getClass().getName();
and I'll get "MyObject", but I would like to be able to get the name given
to that object.
Thanks!
Hal
Manish Pandit - 13 Sep 2006 17:09 GMT
getClass().getDeclaredFields() will give you a Field[], which you can
iterate through and get the names of the field(s) by doing
<field>.getName().
-cheers,
Manish
> I asked a similar question and was able to work it out and get some answers,
> but this one is a bit harder. If I do this:
[quoted text clipped - 12 lines]
>
> Hal
Thomas Kellerer - 13 Sep 2006 17:14 GMT
Hal Vaughan wrote on 13.09.2006 18:04:
> I asked a similar question and was able to work it out and get some answers,
> but this one is a bit harder. If I do this:
[quoted text clipped - 8 lines]
> and I'll get "MyObject", but I would like to be able to get the name given
> to that object.
No you can't (unless it's a member variable not a local one, as described by
Manish).
But in 99% of all cases I'd say: if you need to do that, your design is
seriously flawed
Thomas
Hal Vaughan - 13 Sep 2006 22:14 GMT
> Hal Vaughan wrote on 13.09.2006 18:04:
>> I asked a similar question and was able to work it out and get some
[quoted text clipped - 14 lines]
> No you can't (unless it's a member variable not a local one, as described
> by Manish).
Thanks -- also to all the others who responded.
> But in 99% of all cases I'd say: if you need to do that, your design is
> seriously flawed
Actually, I was just experimenting with a possible debugging aid. I have to
create a wrapper class for Swing components, and while debugging, if there
were an easy way, in one line, to identify the name of the object when
printing out debugging info, it'd be a help. Instead I just added a String
that I set to use for printouts. When I'm done debugging, I'll remove
references to that string.
Hal
Thomas Kellerer - 13 Sep 2006 22:24 GMT
Hal Vaughan wrote on 13.09.2006 23:14:
>> No you can't (unless it's a member variable not a local one, as described
>> by Manish).
[quoted text clipped - 8 lines]
> were an easy way, in one line, to identify the name of the object when
> printing out debugging info, it'd be a help.
OK, that'll be the 1% ;)
Thomas
Hal Vaughan - 14 Sep 2006 07:02 GMT
> Hal Vaughan wrote on 13.09.2006 23:14:
>>> No you can't (unless it's a member variable not a local one, as
[quoted text clipped - 13 lines]
>
> Thomas
And in this case, it was easier to just add a variable to track it. I just
figured if there were an easy way to do it with one line or so, that would
be worth doing. I figured it was worth asking.
One of these days I'm going to have to learn to use debuggers. I know
they're out there, but I didn't even know they existed until about 6-9
months ago. Sometimes there's holes in your background if you're self
taught. ;-)
Hal
David Lee Lambert - 14 Sep 2006 00:23 GMT
>>> I asked a similar question and was able to work it out and get some
>>> answers,
[quoted text clipped - 10 lines]
> were an easy way, in one line, to identify the name of the object when
> printing out debugging info, it'd be a help. Instead [...]
All AWT components (including all Swing components) have the methods
getName() and setName(). However, the default name may not be
meaningful; you might need to do something like this:
MyObject myObj = new MyObject();
myObj->setName("myObj");
Since Java does not have a preprocessor, I don't think it's possible to
write a macro that would instantiate a component and record the
variable-name it was first assigned to; but the above is clear enough.

Signature
PGP key posted on website ... http://www.lmert.com/people/davidl/
Arne Vajhøj - 13 Sep 2006 17:32 GMT
> I asked a similar question and was able to work it out and get some answers,
> but this one is a bit harder. If I do this:
[quoted text clipped - 8 lines]
> and I'll get "MyObject", but I would like to be able to get the name given
> to that object.
I assume you want "myObj".
No.
Because one object may have zero or many names and that info
is not always stored in the byte code.
Arne
Patricia Shanahan - 13 Sep 2006 18:51 GMT
> I asked a similar question and was able to work it out and get some answers,
> but this one is a bit harder. If I do this:
[quoted text clipped - 12 lines]
>
> Hal
myObj is not a name of the object. It is the identifier of a variable
that happens, immediately after its initialization, to contain a pointer
to the MyObject.
The connection between the two of them is very tenuous. Other fields may
refer to the same object at other times. myObj may, at other times, be
null or point to a different instance of MyObject. myObj may go out of
existence at a time when there are other references to the MyObject
instance it originally referenced.
If you wish to associate strings with objects, and be able to find the
associated string given a reference to the object, consider a
Map<MyObject,String>, possibly combined with a map in the other direction.
Patricia
Mark Rafn - 14 Sep 2006 00:49 GMT
>I asked a similar question and was able to work it out and get some answers,
>but this one is a bit harder. If I do this:
> MyObject myObj = new MyObject();
>Is there any way I can, from within myObj, get the *name* of that particular
>object? I know I can do this:
What do you mean by *name* of an object? Name is not a property that
an instance of java.lang.Object has.
> myObj.getClass().getName();
Oh, the name of the class. yes, you can do that.
>and I'll get "MyObject", but I would like to be able to get the name given
>to that object.
Hmm, back to my first question: what do you mean name of the object? Who is
giving this name, and how? The answer to this will likely lead you to the
right way to retrieve this name.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>