articles

Home / DeveloperSection / Articles / Final keyword in java- A complete guide

Final keyword in java- A complete guide

Final keyword in java- A complete guide

Akshay Sharma772 26-May-2022

We use a lot of methods, classes and variables in java. Sometimes we inherit the existing classes or override the methods to reuse them instead of creating new ones, which might increase complexity. But what if we do not want anyone working on the same code with us to inherit specific classes or override specific methods. How do we stop them from doing that? Do we have any keyword that makes the methods, classes and variables unchangeable?  

Yes, Java provides us with a lot of features and keywords. It provides us with a keyword 'final' to act as a non-modifier. We are going to discuss about the “final” keyword in java in this article.

Final keyword

The final keyword in java is a non-access modifier. It restricts us from modifying or changing things. We can implement the final keyword with

=> Class

=> Method

=> Variable

The final keyword, if given before any class, method or variable, prevents the user from re-assigning values or modifying the existing behaviour. Let's learn how to use the keyword in different contexts in this article.

Final keyword with class

We can use the final keyword before the class name. The final keyword when used with a class restricts/prevents the user from inheriting the class. The mechanism of a class extending or acquiring all the properties of another class is called inheritance. Let’s learn how to use the final keyword with a class and try to inherit it in the following example.

class Kiwi extends Fruit{  

  Void execute() {

        System.out.println('Inherited the class safely');

    }

    public static void main(String args[]) {  

        Kiwi obj= new Kiwi();  

       obj.execute();  

   }  

final class Fruit{}  

class Fruit1{}

Output

Final keyword in java- A complete guide

The above example will give us a compile-time error 'cannot inherit from final Fruit' because we are trying to extend a final class Fruit. As the final class is not extendable, the compiler gives us an error. But if we try to extend the class Fruit1 instead of Fruit, we can inherit the class without any restriction.

Final keyword with method

We can use the final word with a method by writing it in the method header before the method name. The final keyword restricts the user from overriding the method. The mechanism of modifying the behavior of the parent class method in the child class with the same method header and parameters is called overriding. Let’s learn how to use the final keyword with a method and try to override it in the following example.

class Fruit{  

final void execute() {

System.out.println('Inside final method execute()');

}  

}     

class Kiwi extends Fruit{  

Void execute() {

System.out.println('overriden the method safely');

}     

    public static void main(String args[]) {  

     Kiwi obj= new Kiwi();  

    obj.execute();  

    }  

Output

Final keyword in java- A complete guide

The above example when executed will give us a compile-time error 'execute() in Kiwi cannot override execute() in Fruit' because we are trying to override a final method, which is not acceptable by Java. As the final method cannot be overridden, the compiler gives us an error. 

Final keyword with variable

We can use the final keyword with a variable too. The variable when declared with the final keyword restricts the user from re-assigning or modifying it. Let's learn how to use the final keyword with a variable and try to modify it in the following example.

class Main{  

  final String fruit = 'kiwi';  

  void execute() {  

    fruit = 'berry';

}  

  public static void main(String args[]){  

   Main obj = new Main();  

   obj.execute();  

  }  

}

Output

Final keyword in java- A complete guide

The above code gives us a compile time error 'cannot assign a value to final variable fruit' because we are trying to re-assign or modify the value of a final variable 'fruit' from 'kiwi' to 'berry'. As any variable with the keyword final doesn't allow us to change it later in the code, we have to declare the final variables for only variables that are used throughout the code with constant value like PI(3.14) value, etc.

Final keyword with Static

The constant variable in Java can be created using both the final and static keywords before a variable name. Static variable tells the compiler that the value of the variable remains the same throughout the code. The final values tells the compiler that the value once assigned cannot be modified. So we can create a constant variable using both the keywords together.

The final static variable word in Java is similar to a constant variable in C/C++. The final keyword when used with java gives the same result as const in C/C++. We declare the variable using const keyword which doesn't allows the user to re-assign any value to it. We can only access the variable and use it in our code, but once we assign any value to the variable, it will become unchangeable.

Note: We are using an object in the main method to access the other methods of the same class. Because all the other methods we have written in those examples are non-static and a non-static method cannot be referenced or invoked from a static context. So we use an object to invoke the methods. 

But if you want to invoke the methods without any object you can make all the other methods static and invoke it using the class name.

Conclusion

The final keyword in java is a non-access modifier. It restricts us from modifying or changing things.

The final keyword java can be used with a class, method or variable.

The final keyword when used with a class restricts/prevents the user from inheriting the class.

The final keyword restricts the user from overriding the method. The mechanism of modifying the behavior of the parent class method in the child class with the same method header and parameters is called overriding.

The variable when declared with the final keyword restricts the user from re-assigning or modifying it.


Updated 26-May-2022
A professionally trained Tech Expert, with great experience in Data Science, SQL, Machine Learning, Python, and Deep Learning, competitive programming, Data Analytics and Web Development.

Leave Comment

Comments

Liked By