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); } }
1 Réponse
+ 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