I was wondering if it was possible to get some String value of an Object to access that Object on the same machine (same RAM) or the same VM via that particular String.
e.g.
Object objA1 = new Object();
System.out.print(objA1.adress); => output: d146a6581ed9e
Object objExt = Object.buildFromMemoryAdress("d146a6581ed9e");
I hope you understand what I'm trying to understand.
EDIT: I found in
http://javapapers.com/core-java/address-of-a-java-object/#&slider1=1
a Class that allows me to get the String of the logical address of an instance on the (VM?) memory: sun.misc.Unsafe
I think I can also use Unsafe to retrieve an Object from the (restricted to the VM?) memory.
If not possible like this, how would I do it, and since it's out of curiosity are there any other languages (especially high end) that allow direct memory access like this?
Absolutely not. In fact, it's clearly impossible, given that you can obviously have two different objects whose
toString()
methods return the same string. As a simple example:What should
x
refer to? The same object thata
refers to, or the same object thatb
refers to?toString()
is not meant to return an object identifier - it's just meant to return some sort of textual representation of an object. Just because the default implementation returns something which looks a bit like an identifier shouldn't be taken as an indication that it should be used as an identifier.If you want to store some way of accessing an object at some other point in time, I suggest you just store a reference to it as an
Object
variable.