top of page
Writer's pictureAbhishek Desai

Final Keyword in Java

The final method in Java is used as a non-access modifier applicable only to a variable, a method, or a class. It is used to restrict a user in Java.


Final keyword is used with below contexts:

  1. Variable

  2. Class

  3. Method


  • Final variable : To create constant variable

  • Final Class : Prevent inheritance

  • Final Method : Prevent Method Overriding


Java Final Variable :

When a variable is declared with the final keyword, its value can’t be changed, essentially, a constant. This also means that you must initialize a final variable.

If the final variable is a reference, this means that the variable cannot be re-bound to reference another object, but the internal state of the object pointed by that reference variable can be changed i.e. you can add or remove elements from the final array or final collection.


It is good practice to represent final variables in all uppercase, using underscore to separate words.


Example:


public static void main(){


final double PI = 3.14159;

}


1) When to use a final variable?

The only difference between a normal variable and a final variable is that we can re- assign the 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 the program.



Java Final Classes :


When a class is declared with the final keyword in Java, it is called a final class. A final class cannot be extended(inherited).


There are two uses of a final class:


Usage 1: One is definitely to prevent inheritance, as final classes cannot be extended. For example, all Wrapper Classes like Integer, Float, etc. are final classes. We can not extend them.


Usage 2: The other use of final with classes is to create an immutable class like the predefined String class. One can not make a class immutable without making it final.


Example:


final class A

{

// methods and fields

}

// The following class is illegal

class B extends A

{

// COMPILE-ERROR! Can't subclass A

}



Java Final Method:


When a method is declared with final keyword, it is called a final method in Java. A final method cannot be overridden.


The Object class does this—a number of its methods are final. We must declare methods with the final keyword for which we are required to follow the same implementation throughout all the derived classes. 


What is a final method in Java?

A final method in Java is a method declared with the final keyword, meaning that its implementation cannot be overridden by subclasses.


Example:


class A

{

final void m1()

{

System.out.println("This is a final method.");

}

}

class B extends A

{

void m1()

{

// Compile-error! We can not override

System.out.println("Illegal!");

}

}



Overall, the final keyword is a useful tool for improving code quality and ensuring that certain aspects of a program cannot be modified. By declaring variables, methods, and classes as final, developers can write more secure, robust, and maintainable code. It provides immutability.

16 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Comments


bottom of page