This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubt about java Class

 
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....
 
lowercase baba
Posts: 12911
63
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?
 
Bartender
Posts: 825
5
Python Ruby Java
  • 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...
 
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
Python Ruby Java
  • 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?
 
Ranch Hand
Posts: 247
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its Like a Puzzle solving session . Did you get the hint leela?
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
increment static variable inside constructor.
 
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

 
Marshal
Posts: 70636
288
  • 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.
 
Marshal
Posts: 25948
69
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
Python Ruby Java
  • 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.
 
Rancher
Posts: 1041
6
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic