Java Final Keyword Example
1. Introduction
In a programming language like Java, the word final is a keyword that is used as an access modifier before variables, methods, and classes. A keyword is a word that has special meaning for the compiler and cannot be used as an Identifier in a language.
The final word can be used before a variable or before a method or before a class in Java. The final Keyword is recognized by the compiler during compilation time. Let us understand the usage of this word in further sections.
2. Using the Java final Keyword
The final keyword can be used in a variable declaration in Java, or a method declaration or a class declaration. Now let us understand how we can use the final keyword in each case declaration and understand it programmatically.
2.1 Final Variables
In the case of primitive variables, if we use the final as an access modifier in its declaration, then that variable value is fixed and cannot be changed throughout the program. If we are trying to changing the value of the final variable then the compiler raises an error. Let us understand this with the below example.
FinalVariable.java
class Test { public static void main(String[] args) { final int a=30; System.out.println("Value of a is:"+a); a=40; //reassign } }
Output
FinalVariable.java:6: error: cannot assign a value to final variable a a=40; ^ 1 error
In the above coding snippet, the value of a is 30 which is declared as final. As we are trying to modify the value at line 6 as 40, the compiler raises an error, which means reassigning final variables is not allowed.
In the case of Object, if we declare any variable as final, the reference of the variable cannot be changed.
FinalRefVariable.java
class Test { public static void main(String[] args) { final StringBuffer sb1= new StringBuffer("CodeGeeks"); StringBuffer sb2= new StringBuffer("Java"); sb1=sb2; //ressigning } }
Output
FinalRefVariable.java:6: error: cannot assign a value to final variable sb1 sb1=sb2; ^ 1 error
In the above coding snippet, we are trying to reassign the reference sb1 to sb2 at line 6 for which the compiler raises an error. So reference cannot be changed once they are declared as final.
Instance variables can be declared as final, once they are declared as final they cannot be modified in the rest of the program. Let us look into this with an example.
FinalInstanceVariable.java
class Test { final String name="Johnson"; //Instance variable public static void main(String[] args) { Test t = new Test(); //creating Test class object t.name="Elizabeth"; //reassign } }
Output
FinalInstanceVariable.java:8: error: cannot assign a value to final variable name t.name="Elizabeth"; //reassign ^ 1 error
In the above coding snippet, we are trying to change the value of instance variable name from Johnson to Elizabeth at line 8. The value of a variable cannot be modified once it is declared as final, so the compiler throws an error.
2.2 Final Methods
In a method declaration, if we use the final, then those methods cannot be overridden. For an instance, if we have a method in the parent class and if we want to restrict the child class to have its own implementation of that method, then the final can be used in the parent’s method declaration.
Let us understand this with the following example:
FinalMethod.java
class Parent { final public void m1() //declaring parent method as final { System.out.println("Parent Class m1 method"); } } class Child extends Parent { public void m1() //trying to override { System.out.println("Child Class m1 method"); } }
Ouput
FinalMethod.java:10: error: m1() in Child cannot override m1() in Parent public void m1() //trying to override ^ overridden method is final 1 error
In the above coding snippet, as we are trying to override m1() method in the child class, the compiler throws an error with the message overridden method, that is, the parent method is final. So, Final methods cannot be overridden.
2.3 Final Classes
In Java, a class can be declared as final. If a class is declared as final, then that class cannot be extended to any other class or in other words that class cannot have any child class or classes.
Let us understand this with the following example:
FinalClass.java
final class A // declaring A class as final { public void m1() { System.out.println("A class m1 method"); } } class B extends A //trying to extend from A class { public void m2() { System.out.println("B class m2 method"); } }
Ouput
FinalClass.java:8: error: cannot inherit from final A class B extends A //trying to extend from A class ^ 1 error
In the above coding snippet, we declared A class as final. When we try to compile the program, the compiler throws an error with a message cannot inherit from final A which means, A class is final and it cannot be extended to any other class like B.
3. Advantages and Disadvantages
The advantage you achieve by using a final keyword in a Java program is security. It ensures security as things cannot be modified but at the same time, the disadvantage is you miss features of object-oriented programming like Inheritance and Polymorphism. Inheritance is missed if a final keyword is used before a class, Polymorphism is missed if a final keyword is used for a method. So it is advisable to use the final keyword in a Java program only if it is absolutely necessary.
4. Download the Source Code
This is an example of Java Final Keyword.
You can download the full source code of this example here: Java Final Keyword Example