Final keyword In Java Explained

Final is a non-access modifier in Java programming language. In Java, the final is used in different contexts. The final Java keyword plays an important role in restricting the access of code using some approaches. 

Final keyword is used with
  1. Data variable
  2. Method
  3. Class

Final Variable:  final variable values are not allowed to change

Both non-primitive and primitive type variables can be marked as final and it will restrict the access of the data variable. Once a programmer marked the final keyword the value of the data variable is not allowed to change after it gets initialized means the data variable value is constant. Here is an example,

Note: The final variable in upper case followed by underscore is good programmer practice 

final int SIZE_OF_BOX=10; //primitive example

The reference variable can be marked as final and this will restrict the reference variable to point to another object or the reference variable allowed to hold the memory address of the object assigned at the time of initialization. But it's possible to make changes in the same object like set or remove the object value. Here is an example,

final Employee EMP_DATA= new Employee(); //non-primitive example

Usually, the Java compiler allows to declare a global variable without any initialization but if final is marked then it should get initialized, or else the compiler will throw an error "The blank final field b may not have been initialized" so it's necessary to initialize the blank final field at declaration. There are two more ways to initialize the final variable in a Java program.

A blank final variable can be initialized in a static block or in an instance intializer block or inside the constructor of the current class. 

//static block and instance initlaizer example

Final Method:  final methods are not allowed to override

A class method can be marked as final and then it is not allowed to be overridden by the child class. Still, the child class can inherit the base class and reuse the final method but is not allowed to override it. This will restrict the child class from doing any modification to the base class method. Here is an example,


class Bank{

final String showBalance() {

return "10$";

}

}

class EuropenBank extends Bank{

String showBalance(){ //compilation error, final method can't be overridden

return "10€";

}

}


When can a method be final?

In a class, if some of the methods are not allowed to be modified by outside class then we can mark final to those methods.

For example, Let's consider a Bank class with the method showBalance() which returns the bank balance in dollars for U.S. customers. If someone creates a European Bank class and overrides the showBalance() method and returns the bank balance in euro or other currency for U.S. customers, then all  European Bank U.S. customers will receive bank balance in euro. This change of behavior affects the basic customer requirements. So we need to disable overriding by declaring final keyword in showBalance() method.

Final Class: final classes are not allowed to be extended /inherited

Final can be applied to a class and then the class is not allowed to be extended by another class. This will disable any other class to modify the final class. Java library contains various final classes that are not allowed to extend by the user's created class and to modify the original behavior. Here is an example,


final class Bank{

}

class EuropeBank extends Bank{//compilation error, not allowed to extend final class

}


When can a class be final?

A class can be marked as final when it's not allowed to be extended. String class in Java is final, the programmer designed this class in a way that it should not extended and to prevent modification in methods like trim(), substring()..etc. Here is an example to extend the inbuilt final class,

class MyString extends String{//compilation error, String is a final class

}

Advantages of final

  • Immutability
  • Method Safety
  • Class Integrity
  • Parameter Safety
  • Thread Safety

Post a Comment

Previous Post Next Post
B