简体   繁体   中英

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?

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.

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM