查询创建多少个对象

public class Test {
    public int id;
    public String name;
    private static long count;
    @SuppressWarnings("rawtypes")
    private final static ThreadLocal tl=new ThreadLocal();

    @SuppressWarnings("unchecked")
    public Test() {
    super();
    count++;
    tl.set(count);
 }
    public long getCount(){
    return (Long)tl.get();
 }
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
        Test t=new Test();
        System.out.println(t.getCount());
 }
}

}

输出

1
2
3

原文地址:https://www.cnblogs.com/96ZYJ/p/4888380.html