mercredi 27 décembre 2017

How to keep track of no of objects created in a java application?

Recently faced this question in an interview:

You have a java application, and it uses a connection object to connect to the different clients. The connection object is of type ClientConnection. I want to know how many live connection objects are present at a particular moment in this application?

Answer given by me:

  1. I will make a static variable connectionCount in ClientConnection class.
  2. Every time ClientConnection constructor is called, I will increment the static variable count by 1.
  3. I will override the finalize() method in ClientConnection and decrements the variable count by 1, every time it is called.

But Interviewer doesn't look satisfied by this answer.
Do we have any other approach for this question?

Aucun commentaire:

Enregistrer un commentaire