final keyword in java
Writing final classes
Declaring an entire class final is allowed in java.If a class is declared as final can’t be subclassed. It is a very useful for immutable classes such as String class in java.
final class FinalClass{ public void methodOne( double var){ //Body of the method } }
Writing final methods
You can use final keyword in a method declaration to indicate that the method can’t be overriddden by subclasses and it is critical to consistent state of th object and one can’t change its behavior. Object class have few final methods that you can’t override in your subclass.
class FinalMethods{ public final void methodOne(double var){ //Body of the method } }
Writing final data fields
Declaring a final data field indicates that variable holds a constant value that can’t be changed. For example value of a PI in Math class.
class FinalDataFields{ final double var; //declaring constant variable public void methodOne(){ //Body of the method } }
Writing final parameter
A formal parameter can be declared as fina, also known as blank final variable which means it is blank until a value is assigned to it and then value in the variable can’t be changed during the life time of the variable. For example :
class FinalParameter{ public void methodOne(final double var){ //Body of the method } }