It's not a secret anymore!*
The moose likes Java in General and the fly likes Doubt about java Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Doubt about java Class" Watch "Doubt about java Class" New topic
Author

Doubt about java Class

leela krishhna
Greenhorn

Joined: Aug 24, 2012
Posts: 10
How to find the Number of objects that are created for a particular class...

Mention any referral example code....
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

Why do you think you can or should be able to do this?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Greenhorn

Joined: Aug 24, 2012
Posts: 10
How increase that static value every time of creating objest..

Please give the code for it...
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Hi,

First you learn use of static varible..After that you can understand 'Kemal Sokolovic' answer.

Before ask question, you need to refer some example in some sites.
I sent some tips you. See your private message.


Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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?
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

Its Like a Puzzle solving session . Did you get the hint leela?


Good luck!!
A small leak can sink a Gigantic ship.>
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

increment static variable inside constructor.
narayana Ega
Greenhorn

Joined: Aug 23, 2012
Posts: 26
declare a static variable inside of your class constructor and make sure that it is increasing its value by 1 for every call of that constructor....

while processing you may destroy some objects for that in finalize() decrease that static variable...

then you will get the count of active instances............
leela krishhna
Greenhorn

Joined: Aug 24, 2012
Posts: 10
Finding no.of Objects created for a class...
I finally got this code...
Any modifications, please suggest

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18711
    
    8

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
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
A sidenote: with multithreading (i.e. concurrently creating several instances) the counter might become inaccurate.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt about java Class