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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…