I just know it can reduce some bugs which are because of careless. But anything else ? Such as JVM efficiency and so on.
Thx all!
I just know it can reduce some bugs which are because of careless. But anything else ? Such as JVM efficiency and so on.
Thx all!
The biggest advantage is it prevents accidental writing into a variable. If a member variable is final, you will get compile time errors if your code tries to reassign that variable.
Of course, if it is an object, you could still change the contents of the object that is referred. But if you follow the principle of using final
fields throughout your code base, then you'd have very few (or none) objects that have any member variables that are modifiable. That will ensure that the object is not mutable once created. And immutable objects prevent a large class of errors. It will prevent a lot of future pain.