srikanth prasad wrote:Without peeping into the code (means not looking for new operator), Can I find how many alive instances are existed for a class?
No; at least not without extra code for every class you need the information for. That's the business of a Garbage Collector - and even then, only for those objects that can be collected when (or if) it decides to do it.
And your friend is wrong. Firstly, such a count will only work for a JVM session (and, as I said above, only for classes that include the code).
Secondly, the use of finalize() is not recommended for reasons that are too numerous to list here; the main two being that:
(a) it adds a disproportionate load to the JVM.
(b) you couldn't be sure at any given point how many of your objects were actually "alive", since finalize() is run by the gc, which you have no control over.
DON'T DO IT.
Isn't it funny how there's always time and money enough to do it WRONG?
Using the finalize() method is not a good solution. The method is not guaranteed to be called. The only thing you know about it is that it is called at most one time.
A slightly better solution is to use a static WeakHashMap, and let each instance store itself as the key; the value can be null. Because the WeakHashMap will remove its keys when they are no longer in use (unlike a regular Map or a List) it will not keep instances alive itself.
That's still not perfect though. First of all, instances will publish themselves before they are fully initialized. Secondly, this is not 100% accurate. Consider:
Try it like this, with line 19 commented out, with line 20 commented out, and with both commented out. You'll see what I mean.
srikanth prasad wrote:Interviewer said this is ok but we can find it other ways also.
There is one possible alternative, if you are only concerned with session counts: have your class implement the java.io.Closeable interface and implement your friend's suggestion through its close() method.
If you document it properly (ie, clients must close objects when they're finished with them) it might help but, at the end of the day, it'll only help you to track those who aren't using your object correctly; and you'll only ever be able to get counts of "unclosed" objects. The rest is down to good design.
srikanth prasad wrote:What are the different ways to find number of alive instances of a class?
That's a very open-ended question, and there's no one right answer. If that's what the interviewer asked, then there are two possibilities:
1. He's a lousy interviewer, or at least didn't put much thought into this particular question.
2. He wants to see about your thought process, give you the chance to explore options or ask for clarification, etc.
If it's #1, then he's probably looking for one specific (and arbitrary) answer. Even though many different answers might be technically correct for different interpretations of that question, he probably will only accept one particular one. Nobody here can know what answer he's expecting.
If it's #2, then there's still not one correct answer, but putting forth different possibilities, indicating their limitations, and asking for clarifications about the actual requirement, would be the way to go.