0

Not understand the code how output comes "2"?

public class Counter { public static int COUNT=0; Counter() { COUNT++; } } public class MyClass { public static void main(String[ ] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); System.out.println(Counter.COUNT); } }

10th Aug 2019, 5:44 AM
Ratnesh Tiwari
Ratnesh Tiwari - avatar
1 Odpowiedź
+ 3
Anytime that a Counter object is created, the static field COUNT is incremented ... Because you created 2 instances, you will get 2 printed when you print COUNT
10th Aug 2019, 5:51 AM
KrOW
KrOW - avatar