Final Keyword in Java with Examples

Introduction

Final keyword in java is basically used for imposing restriction to user.

final keyword is used in many context, which are mentioned below :

  • Variable
  • Method
  • Class

It means, we can use final keyword in variables, methods and classes in Java.

final keyword in java

Let’s learn each in details :

Java Final Variable

Final variables in Java are nothing but constants. Once the final variable is initialized, we cannot change its value.

Declaration : final <data_type> <variable_name> = <value>;

Example : Final Keyword in Java : Final Variable

public class Circle {  
    
 final int RADIUS=30; //declaring final variable  
 
 void draw(){ 
  //trying to change final variable's value
  RADIUS=400;  
 }  
 
 public static void main(String args[]){  
     
 Circle obj=new  Circle();  
 obj.draw();  
 
 }  
}

Output:

Circle.java:7: error: cannot assign a value to final variable RADIUS
RADIUS=400;
^ 1 error 

As in above example, we have declared variable radius as final. So, we are not allowed to change its value. Hence, compile time error is occured.

As per Java naming convention, constants should be declared in CAPS letter.

Blank final variable

As we know, We can apply final keyword in Java with variables.

Blank Final Variables are those final variables which are not initialized at the time of declaration. These are also known as uninitialized final variable or blank final variable.

These type of variables can only be initialized in constructor.

Declaration : final <data_type> <variable_name> ;

Example : Final Keyword in Java : Blank Final Variable

public class Circle {  
    
 final int RADIUS; //declaring blank final variable  
 
  Circle(){ 
  RADIUS=40;  
 }  
 
 void draw(){
     System.out.println("Drawing Circle of Radius - "+RADIUS);
 }
 
 public static void main(String args[]){  
     
 Circle obj=new  Circle();  
 obj.draw();  
 
 }  
}

Output:

 Drawing Circle of Radius - 40 

Now, Let’s see what happens if we try to initialize the blank final variable outside the constructor :

public class Circle {  
    
 final int RADIUS; //declaring blank final variable  

 
 void draw(){
     
     RADIUS=40;  
     System.out.println("Drawing Circle of Radius - "+RADIUS);
 }
 
 public static void main(String args[]){  
     
 Circle obj=new  Circle();  
 obj.draw();  
 
 }  
}

Output:

 Circle.java:8: error: 
 cannot assign a value to final variable RADIUS      
 RADIUS=40;        
 ^ 1 error 

What is the use of Blank Final Variable in Java?

Let’s try to understand it with a real life example :

  • Suppose, we have one Employee class and it has a field Employee_Id.
  • Employee_Id should be final as it is a constant.
  • We cannot initialize the Employee_Id at the time of declaration otherwise each Employee will have same Employee_Id.
  • Thus, we make Employee_Id as Java Blank Final Variable.

Let’s see the below example :

public class Employee {  
    
 final int EMPLOYEE_ID; //declaring blank final variable  

 Employee(int emp_id)
 {
     EMPLOYEE_ID=emp_id;
 }
 
 void print()
 {
     System.out.println(EMPLOYEE_ID);
 }
 
 public static void main(String args[]){  
     
 Employee employee1=new  Employee(1002); 
 Employee employee2=new  Employee(1003);  

 employee1.print();
 employee2.print();
 
 }  
}

Output:

 1002 
 1003 

Static Blank final variable

Java blank final variable can be static as well.

The Static Final variables can not be initialized in constructors. It can only be initialized in static block.

Declaration : static final <data_type> <variable_name>;

Example : Final Keyword in Java : Static Blank Final Variable

public class Employee {  
    
 static final int EMPLOYEE_ID; //declaring blank final variable  

 static
 {
     EMPLOYEE_ID=1234;
 }
 
 void print()
 {
     System.out.println(EMPLOYEE_ID);
 }
 
 public static void main(String args[]){  
     
 Employee employee1=new  Employee(); 

 employee1.print();

 
 }  
}

Output:

 1234 

Java Final Method

Final keyword in Java can be used with methods as well. A Final method in Java cannot be overridden.

It means though sub class can call the final method of parent class but cannot override it.

Declaration : final <return_type> <method_name>(){}

Example : Final Keyword in Java : Final Method

class Shape {  
    
 final void draw()
 {
    System.out.println("Draw.....");
 }

}

public class Circle extends Shape{
 
 void draw()
 {
    System.out.println("Draw Circle.....");
 }
 
 public static void main(String args[]){  
     
 Circle obj=new  Circle(); 

 obj.draw();

 
 }  
}

Output:

 Circle.java:12: error: draw() in Circle cannot override draw() in Shape
 void draw()       
 ^   overridden method is final 
 1 error 

Hence, we cannot override the Java final method.

Now, Let’s look at another example to see if we can call the final method of parent class :

class Shape {  
    
 final void draw()
 {
    System.out.println("Draw.....");
 }

}

public class Circle extends Shape{

 
 public static void main(String args[]){  
     
 Circle obj=new  Circle(); 

 obj.draw();

 
 }  
}

Output:

 Draw..... 

Java Final Class

We cannot inherit the Java Final Class. It means, java class cannot extend any final class.

Declaration : final class <class_name>{}

Example : Final Keyword in Java : Final Class

final class Shape {  
    
 void draw()
 {
    System.out.println("Draw.....");
 }

}

public class Circle extends Shape{

 
 public static void main(String args[]){  
     
 Circle obj=new  Circle(); 

 obj.draw();

 
 }  
}

Output:

Circle.java:10: error: cannot inherit from final Shape 
public class Circle extends Shape{                             
^ 1 error 

Additional Points

  1. Variables declared in an interface in Java are by default final.
  2. Method’s parameters can be declared as final.
  3. final , finally and finalize are three different terms.
  4. Final variables should be declared in Capital letters as per convention. It is not mandatory though.
  5. We can not declare a constructor as final.

Frequently Asked Questions

Q. Is final method inherited?

Yes, It is inherited. We can use it but cannot override it.

Q. Can we initialize blank final variable?

Yes, but in constructor.

Q. What is final parameter?

Any parameter can be declared as final. Its value cannot be changed.

Q. Can we declare constructor as final?

No, It’s not allowed.

Further Readings:

Conclusion

Final keyword in Java is used in three contexts mainly : variable, method and class. It basically imposes restrictions such as no change allowed, no overriding, no inheritance etc.

This is one of the most asked Java interview question.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply