• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about java Class

 
leela krishhna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find the Number of objects that are created for a particular class...

Mention any referral example code....
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you can or should be able to do this?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
leela krishhna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How increase that static value every time of creating objest..

Please give the code for it...
 
Eswar Nec
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
Java MyEclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its Like a Puzzle solving session . Did you get the hint leela?
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
increment static variable inside constructor.
 
narayana Ega
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finding no.of Objects created for a class...
I finally got this code...
Any modifications, please suggest

 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20729
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sidenote: with multithreading (i.e. concurrently creating several instances) the counter might become inaccurate.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic