How to count the number of objects of a class in java
Suppose you want to know how many objects of a class have been created during a program execution. How would you do that? Using constructor When an object of a class is created, its constructor is always called. Thus, if we keep track of how many times the constructor of a class is called, we directly know number of its objects that have been created. Thus, we need to add some counter in a constructor and increment its value by 1 in every invocation. This counter should be a class level variable. Since class level variables have different values for each object, we have toRead More →