public class Age {
private int years;
public void setYears(int y) {
if (y >= 0) years = y;
}
public int getYears() { return years; }
}
// ...
Age a = new Age();
a.setYears(-3);
a.setYears(20);
System.out.println(a.getYears());What is printed?
- A
-3
- Bcheck_circle
20
- C
0
- D
17
Explanation
The negative value is rejected by the validation guard; setting 20 succeeds.