oop - Java `final` method: what does it promise? - Stack Overflow
Why are final variables used in java?
Usage of final
Its use to tag members actually has an additional very special effect - in that at the conclusion of the constructor's execution, all threads will see the same value for all final members. This is not so if it is not marked final.
It's quite possible that the authors were paranoid about an object being read on multiple threads. If you simply mark everything final (and manage the mutability of deep state similarly), synchronization & volatile reads/writes become unnecessary.
This is just speculation on my part, though. It's possible they did it because their IDE told them to, which isn't necessarily a bad thing, I suppose...
Additionally, prior to java 8, in order to use a variable from the enclosing scope within an anonymized inner class method (effectively a bound variable in a lambda), it needed to be marked final. As recently as java 8 (maybe 7?) This explicit requirement was eased into an effectively final requirement. It may be something like that.
For myself:
Member variables - Always mark final if their state doesn't need to change. If state requires change, make plans to manage the state in a safe manner (hopefully by completely encapsulating it inside a single class). Sometimes it can also be accomplished via a mutator which returns a new copy of the object with the change made (and the member is still final).
Member methods - I only mark them final if I absolutely 100% know that this method should not be extended. One needs to be careful with this since it violates the "Open/Closed Principle" of SOLID.
Classes - Same as methods. In practice, I never do this.
Local variables - I suppose it can sometimes be nice to assure future maintainers that a value will not change, but that can be apparent by reading the method. Your methods are relatively short and purposed...aren't they?
Did I miss anything?
More on reddit.comFinal, static, and static final in Java
What is the difference between final class and final method in Java?
Can a final class in Java have final methods?
What is a final class in Java?
Videos
As mentioned, final is used with a Java method to mark that the method can't be overridden (for object scope) or hidden (for static). This allows the original developer to create functionality that cannot be changed by subclasses, and that is all the guarantee it provides.
This means that if the method relies on other customizable components like non-public fields/methods the functionality of the final method may still be customizable. This is good though as (with polymorphism) it allows for partial customization.
There are a number of reasons to prevent something from being customizable, including:
Performance -- Some compilers can analyse and optimise the operation, especially the one without side-effects.
Obtain encapsulated data -- look at immutable Objects where their attributes are set at the construction time and should never be changed. Or a calculated value derived from those attributes. A good example is the Java
Stringclass.Reliability and Contract -- Objects are composed of primitives (
int,char,double, etc.) and/or other Objects. Not all operations applicable to those components should be applicable or even logical when they are used in the bigger Object. Methods with thefinalmodifier can be used to ensure that. The Counter class is a good example.
public class Counter {
private int counter = 0;
public final int count() {
return counter++;
}
public final int reset() {
return (counter = 0);
}
}
If the public final int count() method is not final, we can do something like this:
Counter c = new Counter() {
public int count() {
super.count();
return super.count();
}
}
c.count(); // now count 2
Or something like this:
Counter c = new Counter() {
public int count() {
int lastCount = 0;
for (int i = super.count(); --i >= 0; ) {
lastCount = super.count();
}
return lastCount;
}
}
c.count(); // Now double count
What kind of "contract" does a final method promise?
Look at it the other way, any non final method makes the implicit guarantee that you can override it with your own implementation and the class will still work as expected. When you can't guarantee that your class supports overwriting a method you should make it final.