Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding objects of a class dynamically

 
Arun Ramani
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to find the number of objects and name of the objects of a class dynamically using java reflection API but I am not able to get the object names. Instead of object names, I am getting only the class name that I pass. Could anyone help me out in this regard?
Any help is appreciated.

Thanks
Arun Ramani
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are "object names?" Object don't have names (unless, of course, you give your class a member named "name".)
 
Arun Ramani
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the number of objects of a class and the name given to the object while instantiating the class
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ernest already said, objects don't have names. Perhaps you mean the name of a variable that contains a reference to an object, but if you do this is far from advanced Java. It's a rather elementary confusion.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean the name of a variable pointing to an object? Note that an object can have any number of variables pointing to it, so even if the JVM did track this, the "names" wouldn't be unique. But they're not tracked. There's no notion of the "name" of an object in Java.

In addition, you can't get the number of objects using anything in the standard API; you have to run the JVM in debug mode and use the debugger API to get that information. Otherwise, you can add a static member to count the instances of a class, and increment it in the constructor of that class. But nothing in the reflection API will tell you this.
 
Arun Ramani
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to find the number of objects created in a class of a file say x.java from a different java file say y.java supposing that the location of the x.java file is known.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few ways I can think of.

1) The class can cooperate by counting its own instances
2) You can use the debugger API
3) Use the new JMX APIs in Java 5.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic