• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Tracking number of objects created

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How to track the number of objects created, of a perticular class ?
I mean without using static variables.

Qn is is there any java API which can provide the number of objects created for a perticular class ?

thanks in advance.
Chall.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does not belong in SCJP. Please keep your questions relevant to the forum.
Moving to Java In General (Intermediate).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> How to track the number of objects created, of a perticular class ? I mean without using static variables.

Why do you want it specifically without using static variables? So you probably already know that it's easy with a static member variable.

> Qn is is there any java API which can provide the number of objects created for a perticular class ?

No. The JVM has a debugger API which you can use to monitor a JVM and probably there's also a way to find out how many objects of a specific type the JVM has created, but there's no easy way.
 
Enge Chall
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if there is any Java Reflection API which solves the purpose. So that in the run time a programmer can code to limit the object creation.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Enge Chall:
Hi,
How to track the number of objects created, of a perticular class ?
I mean without using static variables.

Qn is is there any java API which can provide the number of objects created for a perticular class ?

thanks in advance.
Chall.



Everything is possible without static variables. In fact, the very notion of "class-scoped" data is an abstraction leak. The question you might want is "how do I provide global access to some shared piece of information, such as number of instances?" This would require you to pass a callback instance to all constructors. There are many related side issues to this common question, but I'll leave those for you to explore.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you force all clients to create instances only through a factory? The factory can keep count of how many it makes. Of course the factory wants to be a singleton so we probably have at least one static variable somewhere. Was this an academic exercise to do it with no statics at all?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!