The final keyword in Java is a non-access modifier used to apply restrictions on:
- Variables
- Methods
- Classes
Its meaning changes based on where it is used:
| Usage | Meaning |
|---|---|
final variable |
Value cannot be changed (constant) |
final method |
Method cannot be overridden |
final class |
Class cannot be inherited |
A final variable becomes a constant — once assigned, its value cannot be modified.
final int x = 10;
x = 20; // error: cannot assign a value to final variableMust be initialized:
- At declaration
- OR inside constructor
class Demo {
final int a;
Demo() {
a = 100; // allowed
}
}Must be initialized:
- At declaration
- OR inside static block
class Test {
static final int MAX;
static {
MAX = 500; // allowed
}
}Used for constants like:
public static final double PI = 3.14159;Assigned once inside a method.
void show() {
final int x = 50;
}- Must be assigned exactly once.
- Commonly used for constants.
- Helps JVM optimize performance.
- References declared final cannot refer to another object, but object content can still change.
Example:
final Student s = new Student();
s = new Student(); // error
s.name = "Ben"; // allowedA final method cannot be overridden by child classes.
class A {
final void show() {
System.out.println("A show");
}
}
class B extends A {
void show() { } // error: cannot override final method
}- To prevent modification of important behavior.
- Improves security.
- Stabilizes core logic in parent classes.
A final class cannot be extended (no inheritance allowed).
final class A {
void show() { }
}
class B extends A { } // error: cannot inherit final class- To prevent inheritance.
- To create immutable classes.
- For security reasons (e.g., standard library classes).
String class in Java is final.
public final class String { ... }Reason:
- Prevent modification
- Ensure immutability
- Allow safe use in collections
Even if a class is final:
- Its methods can be non-final
- Its variables can be changed
Example:
final class A {
int x = 10; // not final
void display() { }
}Only restriction → class cannot be extended.
class A {
final int x = 10;
}
class B extends A {
// x is inherited but cannot be changed
}class A {
final void show() { }
}
class B extends A {
// show() available but cannot override
}A blank final variable is a final variable not initialized at declaration.
Must be initialized:
- In constructor for instance variable
- In static block for static variable
Example:
class Demo {
final int a; // blank final
Demo() {
a = 200; // must be initialized here
}
}Method parameters can be declared final.
void show(final int x) {
x = x + 10; // error
}Used when the parameter should not be modified.
JVM uses final variables and methods to:
- Inline method calls
- Optimize constant values
- Improve execution speed
Yes. Only overriding is restricted.
No. Static variables belong to class, not object.
No. Only reference can't be changed. Object contents can be modified unless they are final too.
To ensure immutability, security, and allow String Pool optimization.
No — constructors are not inherited or overridden.
final→ keyword (restriction)finally→ block in exception handlingfinalize()→ method used before garbage collection (deprecated)
-
final variable → constant (cannot reassign)
-
final method → cannot be overridden
-
final class → cannot be extended
-
Used for immutability, security, and performance
-
Static final variables are compile-time constants
-
Object referenced by a final variable can still change internally