Shadowing Field Variables
A class constructor and setter method interact with a field that shares a name with a parameter.
What is printed when the following Java program is compiled and run?
class Config {
private int mode = 1;
public void setMode(int mode) {
this.mode = mode + 1;
}
public void reset(int mode) {
mode = 5;
}
public int getMode() {
return mode;
}
}
public class QuestionTrace {
public static void main(String[] args) {
Config c = new Config();
c.setMode(2);
c.reset(10);
System.out.print(c.getMode());
}
}
A
10
B
3
C
1
D
5
Question Leaderboard
Not enough data yet to show leaderboard.
