
The final Java keyword is used to denote a constant value in Java which does not allow users to make any changes in the value once defined. You might have used the final Java keyword when you want a variable to always store the same value once declared.
Once you declare the value of entity i,e. Variable, class or method using final, it cannot be assigned again. In this blog, let us throw some light on this very useful keyword from the list of keywords in Java.
| public class FinalJava_variable{ public static void main(String[] args) { final int MAX_VALUE = 100; System.out.println("The maximum value is " + MAX_VALUE); } } |
| class Final_Method{ public final void display() { System.out.println("This is a final method."); } } class Child extends Final_Method{ // public void display() { // System.out.println("Cannot override this method."); // } } |
| final class MathUtilities { public static double add(double a, double b) { return a + b; } public static double subtract(double a, double b) { return a - b; } } // Attempting to extend this utility class will cause an error: Class ExtendedMathUtilities extends MathUtilities { // Compile-time error: Cannot inherit from final 'MathUtilities' // } public class Main { public static void main(String[] args) { double result = MathUtilities.add(10.5, 5.2); System.out.println("Addition Result: " + result); } } |
| class Car1{ final int speedlimit = 60; void speed(){ speedlimit = 400; } public static void main(String argos[]){ Car1 obj = new Car1(); obj.speed(); } } |
In the above example, we tried modifying the value of our variable, “speedlimit” declared with the final keyword. Hence, it throws a compile time error as changing or modifying the value of a entity declared with the final keyword is not allowed.
| public class FinalVariableExample { // A final instance variable private final int instanceVariable; // A final static variable (constant) private static final double PI = 3.14159; // Constructor to initialize the final instance variable public FinalVariableExample(int value) { this.instanceVariable = value; // The final instance variable is initialized here } public void demonstrateFinalVariable() { // A final local variable final int localVariable = 5; System.out.println("Final local variable: " + localVariable); // Uncommenting the following line would cause a compile-time error: //localVariable = 10; // Error: cannot assign a value to final variable 'localVariable' } public void printValues() { System.out.println("Final instance variable: " + instanceVariable); System.out.println("Final static variable (PI): " + PI); } public static void main(String[] args) { FinalVariableExample example = new FinalVariableExample(20); example.printValues(); example.demonstrateFinalVariable(); // Uncommenting the following line would cause a compile-time error: // PI = 3.14; // Error: cannot assign a value to final variable 'PI' } } |
When we try to modify the value of the final variable, it will throw a compile-time error. If we try to change the value of LocalVariable it will throw a compile time error, as shown below.
When we use the final keyword in Java there are certain things we cannot change or modify in a variable, class or method. Let us check some of the major effects of using final keywords in Java.