public class Widget {
private static int count = 0;
public Widget() { count++; }
public static int getCount() { return count; }
}
// ...
new Widget();
new Widget();
new Widget();
System.out.println(Widget.getCount());What is printed?
- Acheck_circle
3
- B
1
- C
0
- D
Compile-time error
Explanation
The static variable count is shared among all instances; each constructor call increments it, so after three instances count is 3.