• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Finding objects of a class dynamically

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24204
44
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
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 27371
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
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
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
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
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.
 
Lookout! Runaway whale! Hide behind this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic