Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

difficult reference/casting issue

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
US
Hi,

I'm unfortunately forced to contend with the following admittedly strange issue...

I need to somehow save a reference to an object as a string, and later access the object from this string-ified reference. Perhaps an example would be best:

Code:
String s = this.toString(); //"this" is of type "Class"
...
( (Class)s ).ClassMethod();

I've also tried
String s = Integer.toHexString(this.hashCode());

So obviously this is wrong, and I get cast exception errors.
I suspect (but don't know for sure) that the
Object.toString() method preserves information that can be used to reaccess an instantiated object, such as the name and hash info... so:

1) what exactly is the nature of the "Object.hashCode()" data? Does it have anything to do with the address of the object in memory?

2) Is this possible? I think it should be in principle - to be able to cast such a string to an object if all appropriate info (address and type) are available...

3) What about reference counting? If I save the string containing this info (hashCode and type), will the reference count on the object I've "string-ified" be maintained? I.e., will the object not "destruct", if nobody else has a reference to it?

Thanks - I know this is a bit esoteric but unfortunately it's something I've got to contend with (XML is involved, and some other things as well)...

Thanks - help *truly* appreciated!!!
:)
dora






 
Why not create an ObjectBroker pattern in which you can store (and consequently retrieve) objects in a HashTable, using a String key for each object.
 
Thanks for the reply,

I hadn't thought of using an "object broker", and I'm pleased I have a solution! :)

For my own knowledge, however, is it possible to access an object through a "string-ified" reference such as what I've described? I'm always trying to expand my knowledge of Java, and this would definitely fill in some gray areas.

Dora
 
I would think it would not be possible. No matter how you string-ified the object, unless something in your program is holding an actual reference to it, it's eligible to be garbage collected. So it may not be there later when you want to access it again. "When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top