One of hundreds of answers available with our premium content service.
A: To count all instances of a class you should create a static int variable to store the number of instances and include an increment statement in all constructors. You must also implement a final finalize() method that is called upon garbage collection to decrement the count. And since multiple instances may increment and decrement the static value in separate threads, these statements should be enclosed in a synchronized block, as in the example below.
… full answer hidden
Premium members click below for full answer
How can I count the number of instances of an object?