Java final keyword
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 1public 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
The value of pi is 3.14159In 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
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
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. Examplepublic 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
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. Exampleclass 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
Roll no: 915Final 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
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
Test1 Class MethodExample 2
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
Compile time errorThe 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.
final class X { // methods and fields } class Y extends X { // compilation error can’t subclass X }Output
Compile time error.Example 2
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
The type Test1 cannot subclass the final class Test