• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt about java Class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
increment static variable inside constructor.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finding no.of Objects created for a class...
I finally got this code...
Any modifications, please suggest

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A sidenote: with multithreading (i.e. concurrently creating several instances) the counter might become inaccurate.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic