• Post Reply Bookmark Topic Watch Topic
  • New Topic

keep track of how many objects i have created  RSS feed

 
jim dimitriou
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Which is th ebest way to keep track of the number of the objects I've created?
Is is a good practice to have a static variable, which will be incremented everytime I call a contructor?

Is the above a good practice??
(I saw in a friend's code , which, imo is wrong...)

Thanks in advance!
Jimmy
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

A static counter is a possibility, and keeping it private is certainly better than making it public. Both approaches are not thread-safe, though - you may wish to use an AtomicInteger instead of an int (which I assume you meant to use, although you left out the data type in the declaration :-) )
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
Your approach will count how many objects have been created, at least up to 2147483647! It will not, however, keep track of how many objects you no longer have access to.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use a bit more space and show your indentation when you quote code. We have some suggestions here. I added some indentation, and doesn't it look better Spacing and only declaring one variable per line would make it better still.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jim dimitriou wrote:Which is th ebest way to keep track of the number of the objects I've created?
Is is a good practice to have a static variable, which will be incremented everytime I call a contructor?

Actually, I'm not sure that any kind of object counting can really be considered "good practise".

That's supposed to be the JVM's job, not yours. However, if this is for some sort of class assignment (or just for intellectual curiosity), then take Ulf's advice.

The only things I can add are that:
1. It will only keep track of the number of objects you've created, which may be very different from the number of objects there actually are.
2. There is nothing theoretically to stop you from creating more objects than an int (or AtomicInteger) can handle; and the only way you will probably find out is when your counter overflows and you start seeing negative results (Edit: oops, Campbell beat me to it ).

HIH

Winston

PS: Welcome to JavaRanch, jim.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, it sounds like you have been working on those certification exams which ask you questions about how many objects are created by a certain piece of code. Those questions are designed to test your knowledge of how Java works, but in real-life programming you very rarely need to know how many objects you created. (And by "very rarely" I mean "never".)

So like Winston said, if you're just doing this for your own interest, that's fine, it's an interesting exercise, but don't confuse it with something which might be useful for real programs.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!