If you want to know how many instances of the class you defined are created, you can accomplish it by adding a static field to your class definition and increment it each time an instance is created.
Though, I can't really see a good point of doing it...
The quieter you are, the more you are able to hear.
leela krishhna wrote:How increase that static value every time of creating objest..
Ple\ase give the code for it...
I suppose you've already heard of static variables, and know how to use them. If not, I would suggest you go and read about it a little, so you might get an idea what I was talking about.
Anyway, key words are - every time you CREATE an object. Any idea now?
The quieter you are, the more you are able to hear.
That might get you the number of instances created, but you do not know whether any have gone out of scope or been garbage collected.
It will also get you 0 out of 10 for good object‑oriented design. That count variable might be modified outside your Demo class, and you would never know about it.
Campbell Ritchie wrote:That might get you the number of instances created, but you do not know whether any have gone out of scope or been garbage collected.
True, but we don't need to know that. The original question was
How to find the Number of objects that are created for a particular class
leela krishhna wrote:Finding no.of Objects created for a class...
I finally got this code...
Any modifications, please suggest
This seems ok for what you were looking. I would just modify class a little bit, by setting count to be private, and provide just one getter for reading it's value.
The quieter you are, the more you are able to hear.