MIT-Curricular/OOP/Java/Lab/Week7/ObjectCountDemo.java

29 lines
792 B
Java
Raw Normal View History

2024-09-14 01:56:58 +05:30
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());
}
}