Final Keyword In Java

The final keyword in java is used to restrict the user. The java final keyword can be used in many context.

In this tutorial we will learn the usage of final keyword. final keyword can be used along with variables, methods and classes. We will cover following topics in detail.

  1. variable
  2. method
  3. class

The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have detailed learning of these. Let's first learn the basics of final keyword.

1) final variable

final variables are nothing but constants. We cannot change the value of a final variable once it is initialized. Lets have a look at the below code.

class Demo{ final int MAX_VALUE=99; void myMethod(){ MAX_VALUE=101; } public static void main(String args[]){ Demo obj=new Demo(); obj.myMethod(); } }


Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field Demo.MAX_VALUE cannot be assigned at beginnersbook.com.Demo.myMethod(Details.java:6) at beginnersbook.com.Demo.main(Details.java:10)

We got a compilation error in the above program because we tried to change the value of a final variable “MAX_VALUE”.

Note: It is considered as a good practice to have constant names in UPPER CASE(CAPS).

Example of final variable

There is a final variable speedlimit, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

class Bike9{ final int speedlimit=120;//final variable void run(){ speedlimit=520; } public static void main(String args[]){ Bike9 obj=new Bike9(); obj.run(); } }


Output:
Compile Time Error
Blank final variable

A final variable that is not initialized at the time of declaration is known as blank final variable. We must initialize the blank final variable in constructor of the class otherwise it will throw a compilation error (Error: variable MAX_VALUE might not have been initialized).

This is how a blank final variable is used in a class:

class Demo{ //Blank final variable final int MAX_VALUE; Demo(){ //It must be initialized in constructor MAX_VALUE=300; } void myMethod(){ System.out.println(MAX_VALUE); } public static void main(String args[]){ Demo obj=new Demo(); obj.myMethod(); } }


Output:
300
Whats the use of blank final variable?

Lets say we have a Student class which is having a field called Roll No. Since Roll No should not be changed once the student is registered, we can declare it as a final variable in a class but we cannot initialize roll no in advance for all the students(otherwise all students would be having same roll no). In such case we can declare roll no variable as blank final and we initialize this value during object creation like this:

class StudentData{ //Blank final variable final int ROLL_NO; StudentData(int rnum){ //It must be initialized in constructor ROLL_NO=rnum; } void myMethod(){ System.out.println("Roll no is:"+ROLL_NO); } public static void main(String args[]){ StudentData obj=new StudentData(3136); obj.myMethod(); } }


Output:
3136
Uninitialized static final variable

A static final variable that is not initialized during declaration can only be initialized in static block. Example:

class Example{ //static blank final variable static final int ROLL_NO; static{ ROLL_NO=3136; } public static void main(String args[]){ System.out.println(Example.ROLL_NO); } }


Output:
1230
2) final method

A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it.

class Bike{ final void run() { System.out.println("running"); } } class Honda extends Bike { void run(){System.out.println("running safely with 100kmph"); } public static void main(String args[]) { Honda honda= new Honda(); honda.run(); } }


Output:
Compile Time Error
class XYZ{ final void demo(){ System.out.println("XYZ Class Method"); } } class ABC extends XYZ{ void demo(){ System.out.println("ABC Class Method"); } public static void main(String args[]){ ABC obj= new ABC(); obj.demo(); } }


The above program would throw a compilation error, however we can use the parent class final method in sub class without any issues. Lets have a look at this code: This program would run fine as we are not overriding the final method. That shows that final methods are inherited but they are not eligible for overriding.

class Newone{ final void demo(){ System.out.println("XYZ Class Method"); } } class Example extends Newone{ public static void main(String args[]){ ABC obj= new ABC(); obj.demo(); } }


Output:
Newone Class Method
3) final class

We cannot extend a final class. Consider the below example:

final class Bike{} class Honda1 extends Bike{ void run(){System.out.println("running safely with 100kmph");} public static void main(String args[]){ Honda1 honda= new Honda1(); honda.run(); } }


Output:
Compile Time Error
final class PR{ } class ABC extends PR{ void demo(){ System.out.println("My Method"); } public static void main(String args[]){ ABC obj= new ABC(); obj.demo(); } }


Output:
The type ABC cannot subclass the final class PR
Is final method inherited?
class Bike{ final void run(){System.out.println("running...");} } class Honda2 extends Bike{ public static void main(String args[]){ new Honda2().run(); } }


Output:
running...
What is blank or uninitialized final variable?

A final variable that is not initialized at the time of declaration is known as blank final variable.

If you want to create a variable that is initialized at the time of creating object and once initialized may not be changed, it is useful. For example PAN CARD number of an employee.

It can be initialized only in constructor.

Example of blank final variable
class Student{ int id; String name; final String PAN_CARD_NUMBER; ... }


Can we initialize blank final variable?
class Bike10{ final int speedlimit;//blank final variable Bike10(){ speedlimit=90; System.out.println(speedlimit); } public static void main(String args[]){ new Bike10(); } }


Output:
90
static blank final variable

A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Example of static blank final variable
class A{ static final int data;//static blank final variable static{ data=50;} public static void main(String args[]){ System.out.println(A.data); } }


What is final parameter?
class Bike11{ int cube(final int n){ n=n+2;//can't be changed as n is final n*n*n; } public static void main(String args[]){ Bike11 b=new Bike11(); b.cube(5); } }


Output:
Output: Compile Time Error
Points to Remember:
  1. A constructor cannot be declared as final.

  2. Local final variable must be initializing during declaration.

  3. All variables declared in an interface are by default final.

  4. We cannot change the value of a final variable.

  5. A final method cannot be overridden.

  6. A final class not be inherited.

  7. If method parameters are declared final then the value of these parameters cannot be changed.

  8. It is a good practice to name final variable in all CAPS.

  9. final, finally and finalize are three different terms. finally is used in exception handling and finalize is a method that is called by JVM during garbage collection.




Instagram