The best way to explain is an example:
public class Immutable {
private final char[] state = "Hi Mom".getChars();
public char[] getState() {
return state.clone();
}
}
Here we have a properly encapsulated, immutable class. Nothing can change the state (modulo unpleasant reflective tricks).
Now allows JUST to change access to the field:
public class Immutable {
public final char[] state = "Hi Mom".getChars();
public char[] getState() {
return state.clone();
}
}
, getState... ... - :
Immutable mu = new Immutable();
mu.state[1] = 'o';
... .
, private. (, .)
- . , . , ( ) , Immutable. , ; state String. , " /".
, ( public) . public, . ()... . , , , . .