public class Statistics { private static int n; public Statistics() { n++; //每次new一个对象是n+1 } public static int getNum() { return n; //返回n值 } public static void main(String[] args){ Statistics s1=new Statistics(); Statistics s2=new Statistics(); Statistics s3=new Statistics(); Statistics s4=new Statistics();//创建对象 System.out.println("共有对象个数为:"+Statistics.getNum()); } }