Home » Java » Final Keyword In Java

Final Keyword In Java

What is final Keyword In Java?

Final in java is one of the most important keyword and can be applied to class, method, and variables in Java.In this tutorial we will learn the usage of final keyword. We will discuss following topics in details with supporting example.

  • final Variable
  • final method
  • final class

1. Java final variable

Variables that is declared final is called final variable. We cannot change the value of a final variable once it is initialized. A final variable that have no value is called blank final variable or uninitialized final variable. final variables are nothing but constants.Let us consider the code given below.

Example :


class Vehicle{

final int maxSpeed = 120;

void changeSpeed(){

maxSpeed = 150;

}

public static void main(String args[]){

Vehicle obj = new Vehicle();

obj.changeSpeed();

}

}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The final field Vehicle.maxSpeed cannot be assigned

	at com.example.Vehicle.changeSpeed(Vehicle.java:9)
	at com.example.Vehicle.main(Vehicle.java:17)

In the above code we created a int final variable maxSpeed and initialized with a value 120 , In the method changeSpeed we are changing the value of this variable, but value of this variable can't be changed because final variable once assigned a value can never be changed. We will got compilation error in this code.

Blank final variable

A final variable that is not initialized at the time of declaration is known as blank final variable.final variable can be initialized only in constructor otherwise it will throw compilation error.

Example of blank final variable


class Test{

//Blank final variable declaration

final int maxSpeed;

Test(){

//Blank final variable must be initialized in constructor

maxSpeed = 200;

}

void testSpeed(){

System.out.println(maxSpeed);

}

public static void main(String args[]){

Test obj=new Test();

obj.testSpeed();

}

}

Output

200

Real Time use of blank final variable

Suppose we have a Student class which has a field 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. In such case we need to declare roll no variable as blank final variable which will be initialized during object creation.In the case of blank final variable initialization can be done at run time.

static blank final variable

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

Example of static blank final variable :


package com.example;

public class Test{

static final int rollNO;

static{

rollNO = 35;

}

public static void main(String args[]){

System.out.println(Test.rollNO);

}

}

Output

35

final method in Java

A java method with final keyword is called final method. A final method cannot be overridden.You should make a method final in java if you think it’s complete and its behaviour should remain constant in sub-classes. Final methods are faster than non-final methods because they are not required to be resolved during run-time and they are bonded on compile time. Here is an example of final method in Java.


Example of final method


class Animal{

final void run(){System.out.println("Animal is running");}

}

class Tiger extends Animal{

void run(){System.out.println("Tiger is Running");}

public static void main(String args[]){

Tiger tiger= new Tiger();

tiger.run();

}

}

Output:

Compile Time Error

final class in Java

Java class with final modifier is called final class in Java.If you make any class as final, you cannot extend it.Several classes in Java are final e.g. String, Integer etc.

Example of final class in java


final class Loan{

}

class PersonalLoan extends Loan{  //compilation error: cannot inherit from final class
 
}

Important Points to Remember:

  • final variable value can't be changed.
  • A final method cannot be overridden.
  • A final class cannot be inherited.
  • A constructor cannot be declared as final.
  • All variables declared in an interface are by default final.

Previous Next Article

comments powered by Disqus