Variable Shadowing Trap
A method parameter is given the same name as an instance field, causing the parameter to shadow the field during assignment.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
Item item = new Item(20);
item.setVal(50);
System.out.println(item.val);
}
}
class Item {
public int val;
public Item(int val) {
this.val = val;
}
public void setVal(int val) {
val = val;
}
}
A
50
B
0
C
70
D
20
Question Leaderboard
Not enough data yet to show leaderboard.
