Is this a valid way to count instances of objects? Or should I not override finalize?
public class MyClass{
private int instances;
public MyClass(){
instances++;
}
@Override
public void finalize(){
instances--;
}
public static int getInstances(){
return instances;
}
}
The solution doesn't have to be Thread safe
See Question&Answers more detail:os