Skip to main content
Java Output Prediction Medium +20 XP

Static Method Call

What does this print?

JavaRead-only
class Counter {
    static int count = 0;
    Counter() { count++; }
}
new Counter(); new Counter(); new Counter();
System.out.println(Counter.count);

What is the output?

Show answer and explanation

Answer: 3

static variables are shared across all instances. Each new Counter() increments the same count.

Hint: Static is shared, not per-instance.

What this challenge tests

Output Prediction in Java: static, classes.

Related challenges