- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java final Keyword
Usage and Examples
final
Keyword in Java
In Java, the final
keyword is a powerful modifier that brings immutability and restriction to your code. It's used to signal that something should not be changed — whether it’s a variable, a method, or even an entire class. Think of it as Java’s way of saying: "Hands off!"
1. Final Variables – Making Data Immutable
When you declare a variable as final
, you are telling Java: “This variable’s value should be assigned only once.” Once a final variable is assigned, it cannot be modified again.
public class FinalVariableExample {
public static void main(String[] args) {
final int speedLimit = 60;
System.out.println("Speed Limit: " + speedLimit);
// speedLimit = 80; // Uncommenting this will cause a compile-time error
}
}
Speed Limit: 60
Explanation: The variable speedLimit
is marked as final. Attempting to change its value results in a compilation error. This is especially useful for constants or values you want to protect from accidental changes.
2. Final with Reference Types
With objects, final
prevents reassignment — but does not make the object immutable.
class Car {
String model = "Honda";
}
public class FinalReferenceExample {
public static void main(String[] args) {
final Car myCar = new Car();
myCar.model = "Toyota"; // Allowed
// myCar = new Car(); // Not allowed
System.out.println("Car model: " + myCar.model);
}
}
Car model: Toyota
Explanation: The reference myCar
is final, so it cannot point to another Car
object. However, the contents of the object (like its model
) can still be changed.
3. Final Methods – Locking Down Behavior
When you mark a method as final
, it cannot be overridden by subclasses. This ensures that the core logic of the method remains untouched — ideal for methods that form the backbone of a class’s behavior.
class Parent {
final void show() {
System.out.println("Final method in Parent");
}
}
class Child extends Parent {
// void show() { System.out.println("Trying to override"); } // Error!
}
Final method in Parent
Explanation: The method show()
is declared final in the parent class, so any attempt to override it in a child class will lead to a compile-time error.
4. Final Classes – Preventing Inheritance
A final
class is the ultimate boundary — it cannot be extended or subclassed. This is commonly seen in utility or security-related classes.
final class Vehicle {
void drive() {
System.out.println("Driving safely...");
}
}
// class Bike extends Vehicle {} // Compile-time error
Driving safely...
Explanation: Once a class is marked final, it’s sealed. It can still be instantiated, but no subclassing is allowed — this ensures the class's design is preserved as-is.
5. Final Parameters – Read-Only Inputs
You can also declare method parameters as final to ensure that their values are not reassigned within the method.
public class FinalParameterExample {
void display(final int x) {
System.out.println("Value: " + x);
// x = 50; // Not allowed
}
}
Value: [whatever value is passed]
Explanation: This is useful for making sure that parameters passed to methods remain consistent, especially in recursive logic or callback scenarios.
When to Use final
- To define constants (e.g.,
final double PI = 3.14;
) - To avoid accidental modification of variables
- To ensure method behavior is locked for subclasses
- To improve code clarity and intent
- To make classes immutable and non-inheritable