• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding Objects in JVM

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
reply
    Bookmark Topic Watch Topic
  • New Topic