When learning Java, one keyword that often confuses beginners is final
. It’s a simple but powerful modifier that can be applied to variables, methods, and classes. Each use has a slightly different meaning, but the overall idea is the same: final means "no further modification."
In this blog, we’ll break down the different uses of the final keyword with clear explanations and examples.
1. Final Variables
A final variable in Java is a constant—it cannot be reassigned once initialized.
Example:
public class FinalVariableExample {
public static void main(String[] args) {
final int MAX_VALUE = 100;
System.out.println("Max Value: " + MAX_VALUE);
// MAX_VALUE = 200; // ❌ Error: cannot assign a value to final variable
}
}
👉 Once you assign a value to a final variable, you can’t change it again.
👉 This is commonly used to define constants (e.g., PI
, MAX_SPEED
).
💡 If a final variable is not initialized immediately, it must be assigned a value in the constructor (for instance variables) or in a static block (for static variables).
2. Final Methods
A final method cannot be overridden by subclasses. This ensures that the original behavior of the method remains unchanged in any derived class.
Example:
class Vehicle {
public final void start() {
System.out.println("Vehicle is starting...");
}
}
class Car extends Vehicle {
// ❌ Error: cannot override final method
// public void start() {
// System.out.println("Car is starting...");
// }
}
👉 Using final for methods is useful when you want to prevent subclasses from modifying critical functionality.
3. Final Classes
A final class cannot be inherited. This is useful when you want to create an immutable class or prevent further extension.
Example:
final class MathUtils {
public static int square(int n) {
return n * n;
}
}
// ❌ Error: cannot inherit from final class
// class AdvancedMath extends MathUtils { }
👉 One famous example is the String
class in Java—it is declared as final, which means no one can extend it. This makes String
objects immutable and safe to use in multi-threaded environments.
4. Final with Reference Variables
When a reference variable is declared final, it cannot point to a different object after initialization.
However, the object it refers to can still be modified.
Example:
class Person {
String name;
Person(String name) {
this.name = name;
}
}
public class FinalReferenceExample {
public static void main(String[] args) {
final Person p = new Person("John");
p.name = "David"; // ✅ Allowed: modifying object state
System.out.println(p.name);
// p = new Person("Alex"); // ❌ Error: cannot reassign reference
}
}
5. Final vs Finally vs Finalize
It’s easy to confuse these three because they sound similar, but they are different:
- final → Keyword (used with variables, methods, classes).
- finally → Block used in exception handling to execute important code (e.g., closing resources).
- finalize() → Method called by Garbage Collector before destroying an object (rarely used in modern Java).
✅ Key Takeaways
- Final variable → Constant (cannot be reassigned).
- Final method → Cannot be overridden.
- Final class → Cannot be inherited.
- Final reference variable → Cannot point to a new object, but the object can be modified.
Using final properly helps in creating secure, immutable, and stable code.
This Content Sponsored by SBO Digital Marketing.
Mobile-Based Part-Time Job Opportunity by SBO!
Earn money online by doing simple content publishing and sharing tasks. Here's how:
- Job Type: Mobile-based part-time work
- Work Involves:
- Content publishing
- Content sharing on social media
- Time Required: As little as 1 hour a day
- Earnings: ₹300 or more daily
- Requirements:
- Active Facebook and Instagram account
- Basic knowledge of using mobile and social media
For more details:
WhatsApp your Name and Qualification to 9994104160
a.Online Part Time Jobs from Home
b.Work from Home Jobs Without Investment
c.Freelance Jobs Online for Students
d.Mobile Based Online Jobs
e.Daily Payment Online Jobs
Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob