An immutable object is an object whose state cannot be changed. An immutable class is a class whose instances are immutable by design, and implementation. The Java class which is most commonly presented as an example of immutability is java.lang.String.
The following is a stereotypical example:
public final class Person {
private final String name;
private final String ssn; // (SSN == social security number)
public Person(String name, String ssn) {
this.name = name;
this.ssn = ssn;
}
public String getName() {
return name;
}
public String getSSN() {
return ssn;
}
}
A variation on this is to declare the constructor as private and provide a public static factory method instead.
The standard recipe for an immutable class is as follows:
private and final.final to prevent someone creating a mutable subclass of an immutable class.A couple of other things to note:
null can be assigned to a String variable.final, instances are inherently thread-safe. This makes immutable classes a good building block for implementing multi-threaded applications.