Final Keyword in Java

The final keyword in java is used to restrict the user. The java final keyword can be used in many context.

Final can be:

  1. variable
  2. method
  3. class

Final is a non-access modifier applicable only to a variable, a method or a class.

Basically final keyword is used to perform 3 tasks as follows –

  1. To create constant variables
  2. To prevent method overriding
  3. To prevent Inheritance

final keyword in java

Final Variables –

When a variable is declared with final keyword, it’s value can’t be modified,essentially, a constant. This also mean that you must initialize a final variable. It is good practice to represent final variables in all uppercase, using underscore to separate words.

You can initialize a final variable when it is declared. A final variable is called blank final variable,if it is not initialized while declaration. Three ways in which you can initialize final variables –

  1. Initializing when you declare it.
  2. Initializing in constructor.
  3. Initializing in the instance initialization block (IIB)

Lets see a Java Program to see all the different ways in which we can initialize final variables –

Run Online

Difference between Normal variable and Final variable –

The only difference between a normal variable and a final variable is that we can re-assign value to a normal variable but we cannot change the value of a final variable once assigned. Hence final variables must be used only for the values that we want to remain constant throughout the execution of program.

Final Methods –

When a method is declared with final keyword, it is called a final method. A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it. We must declare methods with final keyword for which we required to follow the same implementation throughout all the derived classes.

Java Program Example of Final Method –

Run Online

Output
Compile time error
Final Classes –

When a class is declared with final keyword, it is called a final class. A final class cannot be extended(inherited). 2 reasons to make a class final –

  1. One is to prevent inheritance, as final classes cannot be extended.
  2. To create immutable class (like the predefined String class). You can not make a class immutable without making it final.

Java Program example of Final Class –

Run Online

Output
Compile time error

Points to Remember:
1) A constructor cannot be declared as final.
2) Local final variable must be initializing during declaration.
3) All variables declared in an interface are by default final.
4) We cannot change the value of a final variable.
5) A final method cannot be overridden.
6) A final class not be inherited.
7) If method parameters are declared final then the value of these parameters cannot be changed.
8) It is a good practice to name final variable in all CAPS.
9) finalfinally and finalize are three different terms. finally is used in exception handling and finalize is a method that is called by JVM during garbage collection.

3 thoughts on “Final Keyword in Java

  • May 8, 2018 at 4:31 pm
    Permalink

    Hey Tanmay,
    This is Abhishek Verma,
    Can I add a code compiler in my WordPress site. Like the codecademy and w3schools have.
    Sorry for not being active on your YouTube, as I was busy in my JEE preparation.
    Thanks.
    Abhishek Verma

    Reply
    • May 8, 2018 at 4:37 pm
      Permalink

      Hello Abhishek, yes you can, however there are some security related issues when you host the compiler on your servers. This is something I had read why I wanted this feature so instead, I just point a link yo another online compiler.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *