• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding Objects in JVM

 
Vinayak Sharma
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there a way by which I can get a reference to any object in JVM?

OR

Is there way I can get reference to all the active object instances that are currently available in the JVM ?

OR

Is there a way I can get a reference of all the objects active of a given class in JVM?

For Example :
---------------------

class A {
static void main(String args[])
{
String str = new String("Test");
}
}

class FindAObject {
static void main(String args[])
{
String strObjFound = (String) getReferenceOfObject("str");
}

Object getReference(String objName)
{

/*Steps/API call to get reference to "str" object of Class A*/;

}
}

Here I am assuming that both the classes A and FindObject are active in JVM at a given time. Its really not important to find object by name ... Any other way to find objects will do.

Thanks,
Vinayak
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answers: no and yes.

No, Java does not have functionality for this sort of thing. Arbitrarily retreiving objects from the heap is not safe (They could be garbage collected and then nothing would be there...)

What you could do is implement this yourself, however. A java.util.Map (probably of the HashMap or WeakHashMap variety) will allow you to map two objects together.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...Or depending on how you do this, you could use reflection. (See the java.lang.reflect package). But Reflection would not work in this case, as the "str" variable is a local, not a member, variable.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use the debugger interface (JPDA) and do this from another JVM altogether; this is how modern debuggers and profilers work. Start here to learn about this.
 
Jerry Crothers
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked this question myself in the Advanced Java forum. Some guy gave one idea: declare the variables in a class and use reflection: Class.getField("str");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic