Given:class Counter {
static int total;
int mine;
Counter() {
total++;
mine++;
}
}
public class Main {
public static void main(String[] args) {
new Counter();
new Counter();
Counter c = new Counter();
System.out.println(Counter.total + " " + c.mine);
}
}What is the result?
Choose one.
A static field has one copy per class; an instance field has one copy per object.
Each new Counter() runs the constructor, which increments the class-wide total and the new object's own mine. After three constructions total is 3. The variable c refers to the third object, whose mine went from 0 to 1. The output is 3 1.
- Identify which field is static (total) and which is per object (mine).
- Count constructions: three, so total is incremented three times.
- Each object's mine starts at 0 and is incremented once.
- Print total, a space, then the third object's mine.
Exam tip: Static fields count across all instances; instance fields start fresh in every object.
Java Classes, Records, Constructors and Initializers (1Z0-831) — the lesson that teaches this.