Getting the name of an object

H

Hal Vaughan

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
 
M

Manish Pandit

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
 
T

Thomas Kellerer

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:

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.

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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Hal said:
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.

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
 
P

Patricia Shanahan

Hal said:
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

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
 
H

Hal Vaughan

Thomas said:
Hal Vaughan wrote on 13.09.2006 18:04:

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
 
T

Thomas Kellerer

Hal Vaughan wrote on 13.09.2006 23:14:
Thanks -- also to all the others who responded.


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.

OK, that'll be the 1% ;)

Thomas
 
D

David Lee Lambert

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? [...]

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 [...]

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.
 
M

Mark Rafn

Hal Vaughan said:
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.
 
H

Hal Vaughan

Thomas said:
Hal Vaughan wrote on 13.09.2006 23:14:

OK, that'll be the 1% ;)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top