“Immutable classes are easier to design,
implement, and use than mutable classes.
They are less prone to error...”
-- Joshua Bloch, Effective Java
Immutable objects
- Are simple. They have only one state.
- Are inherently thread-safe; require no synchronization.
- Can be shared freely--no need to make copies.
- Make great building blocks for other objects.
|
class CodeName {
private String code;
private String name;
public CodeName(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
|
|