Why is the instance variable final?

I read this question about immutable objects and left a question regarding immutable objects and the final field:

Why do we need an instance variable in an immutable class?

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 the above code does not have set methods, and the instance variable is set only inside the constructor, why is the instance variable required to be declared final?

+5
source share
3 answers

There is no such requirement to make the variable final . However, when it is true that you clearly intend to never change a variable, it is often good practice to make it final , as it not only provides an invariant against typos or other errors, but also states that you intend to be unchanged, for example, to other programmers or to yourself.

If the variable was public , you would need to make it final to make sure that the interaction code cannot change its value.

Please note that the question related to you is not directly related, as it discusses final classes, not variables. Creating a final class means that it cannot be inherited, and not immutable. Of course, the point is that the content of this question and its answer must be taken into account when creating immutable objects; but nevertheless this is a separate issue.

+5
source

By checking all the fields in the final class, 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 you can easily assume that this class is immutable. What is it, at the moment. But in the end, your class will be modified by another programmer, and this programmer can 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 very easy to inadvertently add a method that changes the state of your object if the fields are not final . By marking them final , this other programmer will receive a compilation error when he tries to change the field, and will have to ask himself why this field is final , and modifying it violates the contract of this class.

It can be argued that Javadoc should be used to document that the specified class is immutable, but frankly, not everyone reads Javadoc. In my opinion, making code is better for yourself.

+4
source

In an immutable class, you cannot change the state of its property / field. Typically, this is done by not providing the setter method to the client code of the class.

And the static final keyword combination is used to create a variable constant. A given variable a variable can never be changed after its initialization.

Since in an immutable class you never need to change the state of a property / field, it is therefore better to make it final.

+1
source

Source: https://habr.com/ru/post/1215944/


All Articles