Am I misunderstanding how private fields work in Java? I was under the impression once a variable is marked as private any other code can not access it, re-assign it, etc.
class EvenNumberHolder {
private int value;
EvenNumberHolder(int value) {
if (value % 2 == 1) {
throw new RuntimeException(value + " is not even");
}
this.value = value;
}
}
void main() {
EvenNumberHolder enh = new EvenNumberHolder(20);
IO.println(enh.value);
enh.value = 22;
IO.println(enh.value);
}
When I re-assign the variable to a new value it updates?