How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
What is an immutable object?
An immutable object is one that will not change state after it is instantiated.
How to make an object immutable?
In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters.
The following class will create an immutable object:
As can be seen in the above example, the value of the
ImmutableInt
can only be set when the object is instantiated, and by having only a getter (getValue
) the object's state cannot be changed after instantiation.However, there must be care taken that all objects that are referenced by the object must be immutable as well, or it could be possible to change the state of the object.
For example, allowing an reference to an array or
ArrayList
to be obtained through an getter will allow the internal state to change by changing the array or collection:The problem with the above code is, that the
ArrayList
can be obtained throughgetList
and be manipulated, leading to the state of the object itself to be altered, therefore, not immutable.One way to get around this problem is to return a copy of an array or collection when called from a getter:
What is the advantage of immutability?
The advantage of immutability comes with concurrency. It is difficult to maintain correctness in mutable objects, as multiple threads could be trying to change the state of the same object, leading to some threads seeing a different state of the same object, depending on the timing of the reads and writes to the said object.
By having an immutable object, one can ensure that all threads that are looking at the object will be seeing the same state, as the state of an immutable object will not change.
You make a class immutable like this:
Immutable classes are useful because they're thread-safe. They also express something deep about your design: "Can't change this." When it applies, it's exactly what you need.
In addition to the answers already given, I'd recommend reading about immutability in Effective Java, 2nd Ed., as there are some details that are easy to miss (e.g. defensive copies). Plus, Effective Java 2nd Ed. is a must-read for every Java developer.
As a non-native English speaker, I dislike the common interpretation of "immutable class" being "constructed class objects are immutable"; rather, I myself lean to interpret that as "the class object itself is immutable".
That said, "immutable class" is a kind of immutable object. The difference is when answering what the benefit is. To my knowledge/interpretation, immutable class prevents its objects from runtime behavior modifications.
Another way to make immutable object is using Immutables.org library:
Immutable classes cannot reassign values after it is instantiated.The constructor assign values to its private variables. Until the object becomes null, values cannot be changed due to unavailability of setter methods.
to be immutable should satisfy following,
Advanteges: Immutable objects contains its initialized values until it dies.
java-immutable-classes-short-note