«Back to Home

Core Java

Topics

Final Keyword In Java

Final Keyword
 
In Java, “final” keyword is very important keyword, which is mainly used for restriction. We can use “final” keyword in many contexts such as variable, method and class.
 
Final variable

When we use “final” keyword with any variable, it is called as a final variable. Final variable can’t be changed. It will be constant.
 
Let’s see an example, given below.
 
Code
  1. public class Employee {  
  2.     final int empId = 101//final variable    
  3.     void work() {  
  4.         empId = 211// Compile time error  
  5.     }  
  6.     public static void main(String args[]) {  
  7.         Employee obj = new Employee();  
  8.         obj.work();  
  9.     }  
  10. }  
41

In the above example, we declare the final variable and initialize it at the time of declaration. Hence, we cannot initialize it again or we cannot change its value. It will be constant.
 
Blank or uninitialized final variable

In Java, blank or uninitialized final variable means a final variable, which is not initialized at the time of the declaration. We can initialize blank final variable only in constructor and if it is a static blank final variable so, it is initialized only in static block.

Let’s see an example of blank final variable, given below.
 
Code
  1. public class Animal {  
  2.     final int age; //blank final variable    
  3.     Animal() {  
  4.         age = 10;  
  5.         System.out.println("Age of animal " + age);  
  6.     }  
  7.     public static void main(String args[]) {  
  8.         Animal a1 = new Animal();  
  9.     }  
  10. }  
42
 
Output

43

Now, let’s see an example of static blank final variable, given below.
 
Code
  1. public class Animal {  
  2.     static final int age; //blank final variable    
  3.     static {  
  4.         age = 10;  
  5.         System.out.println("Age of animal " + age);  
  6.     }  
  7.     public static void main(String args[]) {  
  8.         Animal a1 = new Animal();  
  9.     }  
  10. }  
44

Output

45
 
Final method

When we use “final’ keyword with any method, it is called as a final method. Final method can’t be overridden.
 
Let’s see an example, given below.
 
Code
  1. class Employee {  
  2.     final void work() {  
  3.         System.out.println("Working");  
  4.     }  
  5. }  
  6. public class Manager extends Employee {  
  7.     void work() { // Compile time error  
  8.         System.out.println("working hard");  
  9.     }  
  10.     public static void main(String args[]) {  
  11.         Manager m1 = new Manager();  
  12.         m1.work();  
  13.     }  
  14. }  
46
 
In the example, mentioned above, we declare the final method work (). If we declare any method as a final, we cannot override that method.
 
Final class

When we use “final” keyword with any class, it is called as a final class. Final class can’t be inheritted.
 
Let’s see an example, given below.
 
Code
  1. final class Employee {}  
  2. public class Manager extends Employee { // Compile time error  
  3.     void work() {  
  4.         System.out.println("working hard");  
  5.     }  
  6.     public static void main(String args[]) {  
  7.         Manager m1 = new Manager();  
  8.         m1.work();  
  9.     }  
  10. }  
47

Can final method inherit?

Yes, we can inherit the final method but we cannot override it.
 
Let’ see an example, given below.
 
Code
  1. class Employee {  
  2.     final void work() {  
  3.         System.out.println("Working");  
  4.     }  
  5. }  
  6. public class Manager extends Employee {  
  7.     public static void main(String args[]) {  
  8.         Manager m1 = new Manager();  
  9.         m1.work();  
  10.     }  
  11. }  
48
 
Output

49

We can’t declare a constructor final because constructor can’t be inherited.

Final parameter

If we declare any parameter as final, we can’t change the value of it.
 
Let’s see an example, given below.
 
Code
  1. public class Shape {  
  2.     int cube(final int a) {  
  3.         a = a + 2//can't be changed as n is final    
  4.         a * a * a;  
  5.     }  
  6.     public static void main(String args[]) {  
  7.         Shape s = new Shape();  
  8.         s.cube(7);  
  9.     }  
  10. }  
  11. // Compile time error  
50

Summary

Thus, we learnt “final” keyword is very important keyword, which is mainly used for restriction and also learnt how to use it in Java.