Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

get object name

Thread view: 
finecur - 14 Jul 2007 18:15 GMT
Suppose I have:

List myList = new ArrayList();

What method of myList can I call to return the String "myList"?

Thanks,

ff
Arne Vajhøj - 14 Jul 2007 18:32 GMT
> Suppose I have:
>
> List myList = new ArrayList();
>
> What method of myList can I call to return the String "myList"?

None.

The name of the reference is not part of the object.

And local variables loose the names already during
compilation.

Arne
Lew - 14 Jul 2007 18:42 GMT
> Suppose I have:
>
> List myList = new ArrayList();
>
> What method of myList can I call to return the String "myList"?

None.

Signature

Lew

Patricia Shanahan - 14 Jul 2007 19:38 GMT
> Suppose I have:
>
[quoted text clipped - 5 lines]
>
> ff

"myList" is not a property of the ArrayList at all. A given object can
be referenced by several variables, with different identifiers.

There are several ways you could associate a String with an ArrayList. I
don't know which to recommend. Perhaps you could explain the higher
level problem you are trying to solve?

Patricia
Roedy Green - 14 Jul 2007 23:35 GMT
>List myList = new ArrayList();
>
>What method of myList can I call to return the String "myList"?

the names of local variables don't exist in the class file, so this is
impossible without some preprocessing get it added. However the names
of instance and class methods and variables are in there.

See http://mindprod.com/jgloss/jpda.html
http://mindprod.com/jgloss/debugger.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Piotr Kobzda - 15 Jul 2007 08:53 GMT
>> List myList = new ArrayList();
>>
>> What method of myList can I call to return the String "myList"?
>
> the names of local variables don't exist in the class file,

Don't exist in all class files, because the LVT (LocalVariableTable) is
an optional part of a class file (assumed a part of a debugging info).

> so this is
> impossible without some preprocessing get it added.

Java compiler (javac) can do that with -g switch (-g:vars is enough).

> However the names
> of instance and class methods and variables are in there.

With local variable debugging information present, the names of local
variables (and method parameters) are also in there.  It just requires a
bit more effort to retrieve them, an example is there:
http://tinyurl.com/yqgz3q

piotr
Roedy Green - 15 Jul 2007 00:50 GMT
>List myList = new ArrayList();
>
>What method of myList can I call to return the String "myList"

As others have told you Objects don't have names, and since references
can point to different objects in their career, there is no simple
association between reference name in the source code and Objects.

However, there is a case where Objects can be assigned names. It is a
little used feature. Component.setName.  I used it back in the days of
Java 1.0 to automatically attach labels to fields.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.