-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter.java
More file actions
17 lines (15 loc) · 619 Bytes
/
Counter.java
File metadata and controls
17 lines (15 loc) · 619 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// WAP in Java to create a class called Counter with a Static Variable - count. Implement a constructor that increments count every time an object is created. Print the value of count after creating several objects.
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println("Count = " + count);
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter c4 = new Counter();
System.out.println("Total objects created: " + Counter.count);
}
}