Final Keyword in Java

What is Final Keyword in Java?

The final keyword in Java is used to create entities that cannot be changed once they are initialized. It can be applied to variables, methods, and classes to make them constants, unmodifiable, or unextendable, providing stability and integrity in the program.

How to Initializing a final keyword in Java?

Initializing a final variable in Java can be done in two ways:

1. Initializing at the time of declaration:

final int MAX_VALUE = 70;

In this approach, the final variable MAX_VALUE is declared and initialized with the value 70 in a single line.

2. Initializing within a constructor or instance initializer:

advertisement
advertisement
final int MAX_VALUE;
 
public MyClass() {
    MAX_VALUE = 70;
}

Here, the final variable MAX_VALUE is declared without an initial value. The initialization is done within a constructor or an instance initializer block. Once assigned, the value cannot be changed.

Implementation of Final Keyword using Java

Here is the source code of the Java Program to Illustrate Use of Final Keyword. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  * Java Program to Illustrate Use of Final Keyword
  3.  */
  4.  
  5. import java.util.Scanner;
  6. class Figure
  7. {
  8.     final int length = 5;
  9.     final int bredth = 4;
  10.     final void area()
  11.     {
  12.         int a = length * bredth;
  13.         System.out.println("Area:"+a);
  14.     }
  15. }
  16. class Rectangle extends Figure
  17. {
  18.     final void rect()
  19.     {
  20.         System.out.println("This is rectangle");
  21.     }
  22. }
  23. final public class Final_Use extends Rectangle
  24. {
  25.     public static void main(String[] args) 
  26.     {
  27.         Final_Use obj = new Final_Use();
  28.         obj.rect();
  29.         obj.area();
  30.     }
  31. }
Program Explanation

1. The program starts by importing the Scanner class for user input.
2. The Figure class is defined as the base class with two final variables, length and breadth, representing the dimensions of a figure.
3. The length and bredth variables are declared as final integers, indicating that their values cannot be changed.
4. The area() method in the Figure class calculates and prints the area of the figure.
5. The Rectangle class extends the Figure class and defines the rect() method to display a message indicating that it is a rectangle.
6. The Final_Use class is declared as final, which means it cannot be extended further.
7. In the main() method, an object of the Final_Use class is created.
8. The rect() method is called on the object to display the message “This is rectangle”.
9. The area() method is called on the object to calculate and print the area of the figure.

Program Output:
$ javac Final_Use.java
$ java Final_Use
 
This is rectangle
Area:20
What is final class in Java?

In Java, a final class is a class that cannot be extended or subclassed. When a class is declared as final, it means that no other class can inherit from it. This is achieved by using the “final” keyword before the class declaration.

advertisement

When to use final class?
The purpose of declaring a class as final is to prevent any further modification or extension of its functionality. It provides a way to enforce the integrity and stability of the class implementation. Final classes are often used for utility classes or classes that provide core functionality and should not be altered or overridden.

Examples of final classes in Java include the String class and the Math class. These classes are designed to be used as-is without any modifications or extensions by other classes.

Benefits of using final keyword in Java

The final keyword in Java offers several benefits:

  • Immutable variables: Final allows you to create constants that cannot be changed, ensuring data integrity.
  • Method protection: Final methods cannot be overridden, preserving the original behavior and preventing unintended modifications.
  • Class immutability: Final classes cannot be extended, protecting implementation details and preventing unwanted changes.
  • Compiler optimizations: Final enables the compiler to perform optimizations, resulting in improved performance.
  • Code readability: Using final makes your code more readable by indicating that something should not be modified.
  • Reliability: Final helps prevent accidental modifications, reducing the risk of introducing bugs.
  • Clarity and documentation: Final provides clarity to other developers and helps avoid confusion.
  • Stability and security: Final promotes program stability and enhances security by preventing unauthorized modifications.

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.