aspose file tools*
The moose likes Java in General and the fly likes Finding objects of a class dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Finding objects of a class dynamically" Watch "Finding objects of a class dynamically" New topic
Author

Finding objects of a class dynamically

Arun Ramani
Greenhorn

Joined: Feb 05, 2007
Posts: 5
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

What are "object names?" Object don't have names (unless, of course, you give your class a member named "name".)


[Jess in Action][AskingGoodQuestions]
Arun Ramani
Greenhorn

Joined: Feb 05, 2007
Posts: 5
I want the number of objects of a class and the name given to the object while instantiating the class
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18910
    
    8

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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Feb 05, 2007
Posts: 5
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
 
subject: Finding objects of a class dynamically