Java final keyword

final is a keyword in Java that can be used with instance variables, local variables, methods, and classes.

 

Use of final keyword in Java

1. final variable in Java

A variable declared with the final keyword is known as the final variable. It may be a member variable or a local variable. final variables are constants in Java and they are generally declared with static keywords. As final variables are treated as constants they can’t be reassigned. They are initialized at the time of declaration.

class Test{
    //final variable
    final int num = 100;
    
    //method for try to change the value of final variable.
    public void show(){
        //error because value of final variable can't be change.
        num = 200;
        System.out.println("Num = " + num);
    }
}
public class FinalExample1 {
    public static void main(String args[]){
        //creating object of Test Class
        Test obj = new Test();
        //method call
        obj.show();
    }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final field Test.num cannot be assigned at com.w3schools.business.Test.show(FinalExample1.java:15)
at com.w3schools.business.FinalExample1.main (FinalExample1.java:24)

 

Note: Inside Anonymous classes only final variables are accessible.

 

2.  final method in Java

A method declared with a final keyword is known as the final method.

class Show{
    public final void show(){
        System.out.println("Hello world.");
    }
}

class Display extends Show{
    //error because final method can't be override.
    public void show(){
        System.out.println("Hello w3schools.com.");
    }
}

public class FinalExample2 {
    public static void main(String args[]){
        //creating object of Display class
        Display obj = new Display();
        //method call
        obj.show();
    }
}

Output

Exception in thread "main" java.lang.VerifyError: 
class com.w3schools.business.Display overrides final method show.()V
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.w3schools.business.FinalExample2.main (FinalExample2.java:23)

 

Note: A final method can be inherited but can’t be overridden.

 

3.  final class in Java

A class declared with a final keyword is known as the final class. A final class can’t be inherited.

final class Show{
    public void show(){
        System.out.println("Hello world!");
    }
}
//error because final class can't be inherited.
class Display extends Show{
    public void display(){
        System.out.println("Hello w3schools.blog!");
    }
}

public class FinalExample3 {
    public static void main(String args[]){
        //creating object of Display class
        Display obj = new Display();
        //method call
        obj.display();
    }
}

Output

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
The type Display cannot subclass the final class Show
at com.w3schools.business.Display.(FinalExample3.java:13)
at com.w3schools.business.FinalExample3.main (FinalExample3.java:22)

 

Note: The final method can be inherited.

 

class Show{
    public final void show(){
        System.out.println("Hello world!");
    }
}

class Display extends Show{
    public void display(){
        System.out.println("Hello w3schools.blog!");
    }
}

public class FinalExample4 {
    public static void main(String args[]){
        //creating object of Display class
        Display obj = new Display();
        //method call
        obj.show();
    }
}

Output

Hello world!

 

Note: Abstract class and constructor can’t be final.

4. Blank final variable in Java

A variable declared with the final keyword but not initialized at declaration time is known as a blank final variable. They are initialized at the time of object creation in the constructor and can’t change after that.

class Test{
    //blank final variable which can only be 
        //initialize through constructor.
    final int num;
    
    Test(int n){
        num = n;
        System.out.println("Num = " + num);
    }
}
public class FinalExample5 {
    public static void main(String args[]){
        new Test(100);
    }
}

Output

Num = 100

 

5. static blank final variable in Java

A static variable declared with a final keyword but not initialized at declaration time is known as a static blank final variable. It can be initialized in a static block only.

class Test{
    //blank final variable which can only be initialize 
    //through static initializer block.
    static final int num;
    
    static{
        num = 100;
        System.out.println("Num = " + num);
    }
}
public class FinalExample6 {
    public static void main(String args[]){
        new Test();
    }
}

Output

Num = 100

 

6.  final parameter in Java

A method parameter declared with the final keyword is known as the final parameter. Its value can’t be changed.

class Test{
    public void showDouble(final int num){
        //error because value of final parameter can't be changed.
        num = num * 2;
        System.out.println("Num * 2 = " + num);
    }
}
public class FinalExample7 {
    public static void main(String args[]){
        //creating object of Test class
        Test obj = new Test();
        //method call
        obj.showDouble(10);
    }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The final local variable num cannot be assigned. It must be blank and not using a compound assignment
at com.w3schools.business.Test.showDouble (FinalExample7.java:11)
at com.w3schools.business.FinalExample7.main (FinalExample7.java:20)

 

Advantages/Benefits of final keyword:

  1. Performance: JVM is kept in the cache if variables, methods, and classes are declared final.
  2. Immutable classes: With the help of the final keyword we can make immutable classes.