代码为
1 package CheckObjectNumber; 2 3 public class CheckObjectNumber { 4 private static int num=0;//定义一个静态成员num ,用于检测执行构造函数的次数 5 public CheckObjectNumber() 6 { 7 num++; 8 } 9 public static int getNum()10 {11 return num;12 }13 14 public static void main(String[] args) {15 CheckObjectNumber c1=new CheckObjectNumber();16 CheckObjectNumber c2=new CheckObjectNumber();17 CheckObjectNumber c3=new CheckObjectNumber();18 System.out.println("已建对象为:"+CheckObjectNumber.getNum()+"个");19 }20 21 }
结果为三个;