Final Keyword In Java

Final keyword is used while declaring an entity in java, the entity are classes, variables, methods. if there entities are declared with the final keyword then they cant modified again in the program
Final variables
Once we declare a variable using final, the variable cannot be re modified again in the program,the final variable has a constant value throught the program
Syntax
Example
Example program
Output
In the above example program pi data member in a maths class is declare as a 3.14,so now you cannot change the value of the variable again in the program
Blank final variable
A final variable can be declared with any initialization of the value,but we must intialize a value for the final variable in the constructer of that class
Example
Output
if we didnt initalize the value for the final varibale in the constructer class, program throws a compilation error ,as below
Java final Method
if we declare a method with a final keyword, a child class can access the method but child class method with the same name cannot override the final method
Syntax
Example
Output
In the above program show( ) from a child class (maths2), the class cannot be override Show( ) in the parent class (maths), because of the final keyword.
Java final class
If we declare a class using a final keyword ,then the class cannot be inherited for creating child class
Syntax
Example
Output
In the above example maths(child class ) cannot be created because of final keyword. so maths2 cannot extend maths