29 lines
792 B
Java
29 lines
792 B
Java
class ObjectCount {
|
|
private static int count = 0;
|
|
|
|
public ObjectCount() {
|
|
count++;
|
|
}
|
|
|
|
public static int getCount() {
|
|
return count;
|
|
}
|
|
}
|
|
|
|
public class ObjectCountDemo {
|
|
public static void main(String[] args) {
|
|
System.out.println("Initial count: " + ObjectCount.getCount());
|
|
|
|
ObjectCount obj1 = new ObjectCount();
|
|
System.out.println("Count after creating obj1: " + ObjectCount.getCount());
|
|
|
|
ObjectCount obj2 = new ObjectCount();
|
|
System.out.println("Count after creating obj2: " + ObjectCount.getCount());
|
|
|
|
ObjectCount obj3 = new ObjectCount();
|
|
System.out.println("Count after creating obj3: " + ObjectCount.getCount());
|
|
|
|
System.out.println("Final count: " + ObjectCount.getCount());
|
|
}
|
|
}
|