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.
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?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi