Java final keyword
In this tutorial, we will learn how to use the Java `final` keyword.
In Java, the final keyword is used to define constants and can be applied to variables, methods, and classes. Declaring an entity as final ensures it is only assigned once, meaning:
- A
finalvariable cannot be reassigned. - A
finalmethod cannot be overridden. - A
finalclass cannot be subclassed.
Tip 💡: final means FINAL. No changes, no overrides, no inheritance. It's set in stone! 🗿
1. final Variable in Java
A final variable cannot have its value changed once it has been assigned. For instance:
class Main {
public static void main(String[] args) {
// create a final variable
final int AGE = 32;
// attempt to change the final variable
AGE = 45;
System.out.println("Age: " + AGE);
}
}Can you change the value of a final variable?
Here, we have created a final variable named AGE. We cannot change the value of the final variable.
In this example, the variable AGE is marked final, meaning its value cannot be changed after its initial assignment. Attempting to reassign it will result in a compilation error:
cannot assign a value to final variable AGE
AGE = 45;
^Note
The final keyword is often used to define constants in Java, which are
typically named in uppercase letters.
Blank Final Variable
A final variable that is not initialized at the time of declaration is known as a blank final variable.
If you declare a blank final variable, you must initialize it in the constructor.
class Main {
// blank final variable
final int CAPACITY;
// constructor
Main() {
CAPACITY = 10;
}
public static void main(String[] args) {
Main obj = new Main();
System.out.println(obj.CAPACITY);
}
}2. final Method in Java
A final method cannot be overridden by subclasses. For example:
class FinalDemo {
// define a final method
public final void display() {
System.out.println("This is a final method.");
}
}
class Main extends FinalDemo {
// attempt to override the final method
public final void display() {
System.out.println("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}Here, the display() method in the FinalDemo class is final, so it cannot be overridden in the Main class. Attempting to do so will generate a compilation error:
display() in Main cannot override display() in FinalDemo
public final void display() {
^
overridden method is final3. final Class in Java
A final class cannot be extended by any other class. For example:
// define a final class
final class FinalClass {
public void display() {
System.out.println("This is a final method.");
}
}
// attempt to extend the final class
class Main extends FinalClass {
public void display() {
System.out.println("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}In this example, FinalClass is declared final, so it cannot be subclassed by Main. Attempting to inherit from it will result in a compilation error:
cannot inherit from final FinalClass
class Main extends FinalClass {
^Challenge
Complete this chapter to unlock the next one.
Challenge
Task:
Create a class 'Config' with a final variable 'APP_NAME' set to 'Javapedia'. Print it.Key Takeaways
- Variable: Value cannot be changed (Constant).
- Method: Cannot be overridden by subclasses.
- Class: Cannot be inherited (extended).
Common Pitfalls
[!WARNING] Reassignment: Trying to assign a value to a
finalvariable twice will cause a compile error.
[!WARNING] Confusion: Don't confuse
final(constant) withfinally(try-catch block) orfinalize(garbage collection).
What's Next?
Can a method call itself? Yes! Learn Recursion →
Java Polymorphism
Complete guide to Java polymorphism with examples. Learn method overloading, method overriding, runtime polymorphism, and dynamic method dispatch.
Java Abstract Classes
Learn about Abstract Classes in Java. Understand the difference between abstract and concrete methods, and when to use abstract classes vs interfaces.
