I read this question about immutable objects and was left with a question regarding immutable objects and final field:
Why do we need instance variable in immutable class to be final?
For example, consider this immutable class:
public final class Immutable
{
private final int someVal;
public Immutable(int someVal)
{
this.someVal= someVal;
}
public int getVal() {
return val;
}
}
If in the above code there is no set methods and the instance variable is set only within the constructor, why is it required that the instance variable is declared final?
The previous answers are wrong.
To be immutable an object must declare all properties as
final
.There are certain guarantees that the Java Memory Model makes for final fields that protect them from returning incorrect default values when there are data races between threads.
See example 17.5-2 in https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.5-110.
In immutable class you can not change the state of it's property/field. Generally it is done by not providing setter method to the client code of the class.
And the combination of
static final
keyword is used to make a variable constant. A variable declared final can never be change it's value once it has been initialized.Since in immutable class you never need to change the property/field state, so it is better to make it final.
There is no requirement as such to make the variable
final
. However, when it is true that you explicitly intend to never change the variable, it is often good practice to make itfinal
, since that not only enforces the invariant against typos or other mistakes, but also declares the intent that it be immutable to, eg., other programmers or yourself.If the variable were
public
, then you would strictly need to make itfinal
in order to ensure that interfacing code cannot change its value.Do note that the question you link is not directly related, however, as it discusses classes that are
final
, rather than variables. Making a classfinal
means that it cannot be inherited from, not that it is immutable. There is certainly a case to be made for taking heed of the contents of that question and its answer when making immutable objects, however; but it is a separate question nonetheless.By marking all of a class' fields
final
, you make it clear that you intend your class to be immutable.Suppose you have the following class:
There is no setter method, so one could easily assume that this class is immutable. Which it is, for now. But eventually your class will be modified by some other programmer, and this programmer might add some methods to your class:
It is quite easy to inadvertently add a method that modifies the state of your object if the fields are not
final
. By marking themfinal
, this other programmer will get a compile error when he tries to modify the field, and will have to ask himself why is this fieldfinal
, and does his modification breaks the contract of this class.One could argue that the Javadoc should be used to document that said class is immutable, but quite frankly, not everyone reads the Javadoc. Making the code speak for itself is better in my opinion.