We use the Java final keyword in order to restrict a user. It is a non-access modifier applicable only to a variable, a method or a class. It is used in different contexts. These are:
- Final Variable Used to create Constant Variables
- Final Methods Used to prevent Method Overriding
- Final Classes Used to prevent Inheritance
Final Variable in Java
If any variable is declared with the final keyword, then we cannot change the value of that variable. Hence that variable behaves like a constant. Hence we must initialize a final variable. In order to differentiate between the final variable and normal variables, we can prefer to write the final variable in CAPITALS for easier differentiation between the two.
For example:
final int MAX = 5;
This declares a final variable MAX which is initialized to a value of 5.
A final variable that has no value is called a blank final variable or uninitialized final variable. These can be initialized in a constructor only.
For example:
final int MAX;
This only declares a final variable MAX which is not having any initial value.
Initializing a final variable in Java
It is necessary to initialize a final variable. If we do not do so, the compiler will throw a compile-time error. A final variable can be initialized only once. There are three ways of initializing a final variable. These are as follows:
- The first and the most common approach is that we can initialize a final variable when it is declared.
- In case a variable is not initialized then we can initialize it inside a constructor. If more than one constructor is there in a class then the final variable must be initialized in all of them or a compile time error will be shown on screen.
- A blank variable can be initialized in a static block.
For example:
class Eg { final int MAX; // final variable declared Eg() { MAX = 50; // final variable initialized } void show() { System.out.println("Value of MAX = " + MAX); } public static void main(String args[]) { Eg obj = new Eg(); obj.show(); } }
Output
Java Final Method
When a method is declared with the final keyword, it becomes a final method. A final method is inherited but it cannot be overridden by its subclass though the subclass can make a call to the final method. If the child class tries to override such a method then a compile time error will be shown on the screen.
For example:
class finalEg { final void demo() { System.out.println("Demo final method"); } } class finalMthd extends finalEg { void demo() { System.out.println("Main method"); } public static void main(String args[]) { finalMthd f = new finalMthd(); f.demo(); } }
Output
Since we try to override the function demo in the above example, an error message is flashed on the screen. Now, let us see what happens when we do not override it. Have a look at the following code.
class finalEg { final void demo() { System.out.println("Demo final method"); } } class finalMthd extends finalEg { public static void main(String args[]) { finalMthd f = new finalMthd(); f.demo(); } }
Output
Java Final Classes
A final class cannot be extended by us. Let’s have a look at an example.
Example
final class finalEg { } class finalClass extends finalEg { void demo() { System.out.println("Main method"); } public static void main(String args[]) { finalClass f = new finalClass(); f.demo(); } }
Output
A few Points to Remember about Java Final Keyword
- A constructor cannot be declared as final.
- Local final variable must be initialized during declaration.
- We cannot change the value of a final variable.
- A final method cannot be overridden.
- A final class cannot be inherited.
- It is a good practice to name the final variables in CAPITALS.