Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why instance variable to be final?

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?

like image 394
Naz Avatar asked Mar 23 '15 20:03

Naz


2 Answers

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 it final, 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 it final 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 class final 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.

like image 123
Dolda2000 Avatar answered Oct 04 '22 20:10

Dolda2000


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:

public class Immutable {
    private int value;

    public Immutable (int value) {
        this.value = value;
    }

    public int getValue () {
        return value;
    }
}

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:

public class Immutable {
    private int value;

    public Immutable (int value) {
        this.value = value;
    }

    public int getValue () {
        return value;
    }

    public void doSomething () {
        value++;
    }
}

It is quite easy to inadvertently add a method that modifies the state of your object if the fields are not final. By marking them final, this other programmer will get a compile error when he tries to modify the field, and will have to ask himself why is this field final, 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.

like image 41
Laf Avatar answered Oct 04 '22 19:10

Laf