Category: java
final keyword
Published on 18 Feb 2026
Explanation
final is a keyword in Java used to
restrict modification. It can be applied to variables,
methods, and classes.
Code:
final int x = 10;
Explanation
A final variable cannot be reassigned once it
is initialized.
Code:
final int number = 100; // number = 200; // Compilation Error
Explanation
A final method cannot be overridden by a
subclass.
Code:
class Parent {
final void display() {
System.out.println("Final Method");
}
}
class Child extends Parent {
// void display() {} // Error
}
Explanation
A final class cannot be extended by another
class.
Code:
final class Animal {
}
// class Dog extends Animal {} // Error