Final keyword in Java

The Final keyword is a non-access modifier in java and it can be applied to
  1. variable(final variable)
  2. method(final method)
  3. class(final Class)

Final Variable

If any variable in java class is declared with the final keyword is known as the final variable.  the final variable value can't be modified once initialized. that means we can access the final variable value but we cannot modify the final variable value. The final variable value will be constant. Let's see the below code

 Example

public class FinalDemo {

 private final int x = 10;
 public void modify(){
  x = 20;
  System.out.println(x);
 }
 public static void main(String[] args) {
  FinalDemo f = new FinalDemo();
  f.modify();
  

 }

}
output

From above we can confirm that we cant modify the final variable value.in simple terms we can say that final variable values are constant values.

 Example of accessing the final variable


public class FinalDemo {

 private final int x = 10;
 public void modify(){
  
  System.out.println(x);
 }
 public static void main(String[] args) {
  FinalDemo f = new FinalDemo();
  f.modify();
  

 }

}
output



Final class

a final class cannot be subclassed. That means Any java class cannot extend the final class so we can say that the inheritance mechanism is not applicable for final classes. if we try to extend any final class we will get a compile-time error. Have a look at below example
Example


public class FinalDemo {

 private final int x = 10;
 public void modify(){
  
  System.out.println(x);
 }
 public static void main(String[] args) {
  FinalDemo f = new FinalDemo();
  f.modify();
  

 }

}
public class Finalchild extends FinalDemo {
 
 public static void main(String[] args) {

  System.out.println("hello world");

 }

}
output
The type Finalchild cannot subclass the final class FinalDemo

Final Method

final methods cannot be overridden in subclasses. That means if you have any final methods in Super(Parent) class those final methods cannot be overridden in Subclass(child class). So We can say that by using final keyword we can restrict the method overriding
Example
package com.myPractice;

public class Parent {
 public final void parentMethod(){
  System.out.println("im final method");
 }

}

public class Child extends Parent {
 public void parentMethod(){
  System.out.println("im not overridden");
 }
 
 public static void main(String[] args) {

  System.out.println("hello world");

 }

}
We will get here compile-time error saying

Exception in thread "main" java.lang.VerifyError: class com.myPractice.
Child overrides final method parentMethod.()

here a point to note that we cannot override the superclass method but we can access the final method as it is.
Example
package com.myPractice;

public class Parent {
 public final void parentMethod(){
  System.out.println("im final method");
 }

}
public class Child extends Parent {
 
 
 public static void main(String[] args) {

  Child child = new Child();
  child.parentMethod();
 }

}

Output

im final method

What is Blank Final Variable?

Blank Final variable was introduced in java 1.1v. blank Final variable means it is a final variable which is not initialized at the time declaration and it can be initialized only in the constructor.
Example
package com.myPractice;
public class Child  {
 private final int x;
 
 public Child() {
  x = 10;
  System.out.println(x);
 }



 public static void main(String[] args) {

  Child child = new Child();
  
 }

}
output
10
Important ponints about Final keyword
 final variable value can't be modified once initialized.
The final keyword restricts the method overriding.
The final keyword restricts inheritance.