Java Final keyword

Java Final keyword

Final is a nonaccess 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 is declared with final keyword is known as final variable which means 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. final variable value will be constant.

 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 varible values are constant values.
 Example for accessing 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

final class cannot be subclassed. That means Any java class cannot extend 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 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 that final methods cannot be overridden in Subclass(child class). So We can say that by using final modifiers 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 super class method but we can access 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
-->