Java final keyword
The ‘final’ keyword is a non-access modifier. It is used to restrict the user to manipulate the data members (variables, methods, and classes).
It is used in different context like
- Variables
- Methods
- Classes
Final variables
A variable which is declared with the ‘final’ keyword is called a final variable. If you want to declare a variable as final then prefix a keyword ‘final’ with it. Like final int a, final string s, final char c, etc.
We can’t modify the value of a final variable although we can re-assign value to a normal variable. So we use the final variable for the values which we want to remain constant throughout the program. A final variable should be initialized.
Let’s take a tour with the following examples
Example 1
1 2 3 4 5 6 7 8 9 10 |
public class Test { public static final double PI=3.14159; public static void main(String[] args) { System.out.println("The value of pi is "+PI); } } |
Output
1 2 3 |
The value of pi is 3.14159 |
In the given example, we made the value of pi as final, so it can’t be changed or modified in this program. If we do so, then it will produce a compilation error.
Let’s have a look at another example.
Example 2
1 2 3 4 5 6 7 8 9 10 11 |
class Car{ final int speed=120;//final variable void run(){ speed=250; } public static void main(String args[]){ Car c1=new Car(); c1.run(); } |
Output
1 2 3 |
Compile time error. |
Blank final variable
A variable which is not initialized at the declaration time is known as the blank final variable. We must initialize a variable either at the time of declaration or in the constructor of the class; otherwise, it will throw a compilation error. Like var_name might not have been initialized.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class Test { final double PI; Test() { PI=3.14159; } void method() { System.out.println("The value of pi is "+PI); } public static void main(String[] args) { Test t=new Test(); t.method(); } } |
Output
1 2 3 |
The value of pi is 3.14159 |
Use of blank final variable
Let’s assume we have a college data which is containing a field called Roll_no. Since Roll_no is a fixed entity, so it should not be changed once the student is registered. We can declare Roll_no as a final variable in a class, but we can’t initialize it in advance for all students, because all students have a unique value. In such a case, we can declare roll_no as a blank final variable, and we will initialize this value during object creation.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Student{ final int roll_no; Student(int r){ roll_no=r; } void Method(){ System.out.println("Roll n: "+roll_no); } public static void main(String args[]){ Student obj=new Student(915); obj.Method(); } } |
Output
1 2 3 |
Roll no: 915 |
Final methods
When a method declared with a ‘final’ keyword it is called as final method. If a method declared as final then we can’t override it.
We must declare methods as final when we require the same implementation throughout the program.
Let’s have a look at the below example to understand the final keyword with methods.
Example 1
We can use the parent class final method in child class without any issue
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Test1{ final void demo(){ System.out.println("Test1 Class Method"); } } class Test extends Test1{ public static void main(String args[]){ Test t= new Test(); t.demo(); } } |
Output
1 2 3 |
Test1 Class Method |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Test1{ final void demo(){ System.out.println("Test1 Class Method"); } } class Test extends Test1{ void demo() { System.out.println("Test class method"); } public static void main(String args[]){ Test t= new Test(); t.demo(); } } |
Output
1 2 3 |
Compile time error |
The given example will throw the compile-time error because we are trying to override a final method. However, we can use the parent class final method in child class without any issue (as in example 1).
Final classes
A class declared with final keyword is known as final class. A final class can’t be inherited. The final keyword with class prevents it to be inherited.
We create a class final to prevent it from being inherited and make it immutable.
Uses of the final class
There are mostly two uses of a final class.
- One is to restrict a class to be inherited.
- And the other use is to make a class as immutable like predefined String class.
Example 1
1 2 3 4 5 6 7 8 9 10 |
final class X { // methods and fields } class Y extends X { // compilation error can’t subclass X } |
Output
1 2 3 |
Compile time error. |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 |
final class Test{ } class Test1 extends test{ void demo(){ System.out.println("in method demo"); } public static void main(String args[]){ Test1 t= new Test1(); t.demo(); } } |
Output
1 2 3 |
The type Test1 cannot subclass the final class Test |