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.
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:
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.
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.
/*
* Java Program to Illustrate Use of Final Keyword
*/
import java.util.Scanner;
class Figure
{
final int length = 5;
final int bredth = 4;
final void area()
{
int a = length * bredth;
System.out.println("Area:"+a);
}
}
class Rectangle extends Figure
{
final void rect()
{
System.out.println("This is rectangle");
}
}
final public class Final_Use extends Rectangle
{
public static void main(String[] args)
{
Final_Use obj = new Final_Use();
obj.rect();
obj.area();
}
}
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.
$ javac Final_Use.java $ java Final_Use This is rectangle Area:20
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.
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.
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”.
- Check Java Books
- Practice Programming MCQs
- Apply for Java Internship
- Practice Information Technology MCQs
- Apply for Computer Science Internship