Click to See Complete Forum and Search --> : Number of objects in Java?


mikeshn
06-11-2002, 11:20 AM
Does it possible in Java to use come command or function to find how many objects or instances were created when a program was running?


Thanks in advance

bwkaz
06-11-2002, 01:05 PM
Not that I know of (but a search of the documentation/the API on java.sun.com might turn up something....), but if the objects are all of the same type, then you can just create a static member of that class (static int count;) and increment it in every constructor. Then, when you need to know what the total number of objects that have been created so far is, you just check that static variable. Before your program closes, print that number.

If they're of different types, though, that won't work.

[ 11 June 2002: Message edited by: bwkaz ]