Shared Job Counter
A printer class utilizes a static variable to track the total number of instances created.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
Printer p1 = new Printer();
Printer p2 = new Printer();
System.out.print(p1.getCount() + "" + p2.getCount());
}
}
class Printer {
private static int count = 0;
public Printer() {
count++;
}
public int getCount() {
return count;
}
}
A
22
B
12
C
11
D
21
Question Leaderboard
Not enough data yet to show leaderboard.
