The final keyword in Java is a non-access modifier that prevents reassignment, overriding, or inheritance depending on where it is applied. Use it on a variable to fix its value, on a method to block overriding, or on a class to stop subclassing entirely. The compiler enforces all three rules before the program runs.
- final variable: value cannot be reassigned once initialised.
- final method: cannot be overridden by any subclass.
- final class: cannot be extended;
Stringin Java is a classic example. final,finally, andfinalizeare three completely different things despite their similar names.- Understanding the
finalmodifier is a common topic in BTech, BCA, and MCA Java interviews across India.
What final Means in Java
What is the final keyword in Java, at its core? It is a compile-time contract. You are telling the JVM: this entity is done, do not touch it. The compiler enforces that contract before your code ever runs, so mistakes surface early rather than as runtime surprises during a production incident.
Java has held a top-four position on the TIOBE Index consistently since 2002, reflecting its dominance in enterprise and Android development. According to the Stack Overflow Developer Survey 2024, Java is used by approximately 30% of professional developers globally, making its modifier system one of the most widely encountered in the industry. A JetBrains State of Developer Ecosystem 2023 report found that Java is the primary backend language for 48% of backend developers surveyed, underlining why mastering features like final matters for career readiness.
The final modifier sits alongside static, abstract, and synchronized as part of Java’s modifier system. If you are just getting started with programming languages and tools, our guide on software development tools to learn first gives useful context before you go deeper into Java specifics.
Syntax at a Glance
| Applied To | Syntax Example | Effect |
|---|---|---|
| Variable | final int MAX = 100; |
Value cannot be reassigned |
| Method | public final void display() {} |
Cannot be overridden in subclass |
| Class | public final class Config {} |
Cannot be subclassed |
| Parameter | void calc(final int x) {} |
Parameter value fixed inside method |
final Variables
A java final variable must be assigned exactly once. After that, any attempt to reassign it causes a compile-time error, not a runtime exception. That is a meaningful distinction: you catch the bug before deployment, not during a production incident.
There are three kinds of final variables: local variables, instance variables, and static final variables. Static final variables are what Java uses for constants. You will see this pattern constantly in real codebases:
public static final double PI = 3.14159;
That is the final in java with example pattern you will see in every Java textbook and interview. The static makes it belong to the class; the final makes it immutable. Together they produce a true compile-time constant.
Blank Final Variables
A blank final variable is declared without an initial value. You must assign it in the constructor before the object is created, or the compiler refuses to compile. This is useful when the constant’s value depends on constructor arguments.
One common mistake beginners make is declaring a final variable inside a loop and expecting to reassign it on each iteration. That throws a compile error immediately. The fix is to remove the final modifier if the variable genuinely needs to change, or restructure the logic so a new final variable is created per iteration scope.
final with Object References
Making an object reference final does not make the object itself immutable. The reference cannot point to a different object, but the object’s internal state can still change. A final ArrayList can still have items added to it. True immutability in Java requires immutable fields inside the object as well.
final Methods and Classes
A final method in Java cannot be overridden by any subclass. It can still be inherited and called; it just cannot be redefined. You use this when you want to guarantee that a specific implementation stays intact regardless of how many layers of inheritance exist below it.
The use of final keyword in Java at the method level is common in security-sensitive code. If a method handles authentication logic, you do not want a subclass silently replacing it with a weaker version. Indian fintech platforms built on Java, including UPI-based payment systems developed by NPCI and integrated by apps like PhonePe and Paytm, rely on exactly this kind of design discipline to protect transaction integrity.
What Is a final Class in Java
A final class in Java cannot be subclassed at all. Java’s own String class is final. So are the wrapper classes Integer, Double, and Boolean. According to the Oracle Java documentation, making a class final is a deliberate design choice to prevent unintended extension that could break the class’s invariants.
If you try to write class MyString extends String {}, the compiler rejects it instantly. The String class has very specific behaviour around immutability and interning, and allowing subclasses would break it. This is why understanding the final class concept is tested in viva examinations at universities including Anna University, Mumbai University, and VTU.
If you are studying for a Java interview as part of MCA or BTech placement preparation, check what jobs and salary after MCA look like so you know which concepts actually come up in technical rounds.
final in C++ vs final in Java
The final keyword in C++ was introduced in C++11 and works similarly but with slightly different syntax. In C++, you apply final to a class or a virtual function to prevent further overriding or inheritance. In Java, final is broader: it applies to variables too, which C++ handles differently through const. If you are coming from a C background, our guide on how to write, compile and run a C program shows how C handles constants differently from Java’s final modifier.
The conceptual goal is the same in both languages: signal intent, prevent misuse, and let the compiler catch errors early. Java’s implementation is arguably more consistent since one keyword covers variables, methods, and classes.
When to Use final in Java: Best Practices
Use final on a variable when its value must never change after assignment. Use it on a method when the implementation must be preserved across all subclasses. Use it on a class when the entire type must be closed to extension. Do not add final everywhere chasing performance gains; the HotSpot JIT compiler already inlines non-overridden methods. Use it where it genuinely expresses design intent.
Students exploring the difference between application-layer roles will find our comparison of full stack developer vs software developer useful for understanding where Java fits in different career paths.
final vs finally vs finalize
These three words look related but they have nothing to do with each other functionally. Confusing them is one of the most common Java interview mistakes, especially for candidates from tier-2 and tier-3 colleges in India preparing for campus placements.
| Keyword / Method | Type | Purpose | Can Be Skipped? |
|---|---|---|---|
final |
Modifier | Prevents change, override, or extension | Yes, it is optional |
finally |
Block (exception handling) | Runs cleanup code after try-catch regardless of outcome | No, if present it always runs |
finalize() |
Method (Object class) | Called by GC before object is collected; deprecated since Java 9 | Yes, and you should avoid it |
finally is part of Java’s exception handling system. It guarantees that a block of code, typically resource cleanup like closing a file or database connection, runs whether or not an exception was thrown. If you want to understand how exceptions work in depth, our article on Java exceptions and common runtime errors covers it thoroughly.
finalize() was a method on the Object class meant to let objects clean up before garbage collection. Oracle deprecated it in Java 9 and it is effectively dead in modern Java. Do not use it in new code.
If you are studying BSc Computer Science subjects and scope, Java is typically covered in your second or third semester and these three terms come up in practicals and viva examinations almost every year across Indian universities.
Frequently Asked Questions
What is the final keyword in Java?
The final keyword in Java is a non-access modifier that restricts further modification. When applied to a variable, it prevents reassignment. When applied to a method, it prevents overriding. When applied to a class, it prevents inheritance. It is a compile-time check, meaning errors are caught before the program runs.
What is a final class in Java?
A final class cannot be subclassed by any other class. Java’s built-in String, Integer, and Boolean classes are all declared final. This design prevents unintended changes to their behaviour through inheritance and protects the integrity of their internal implementation.
Can a final method be overridden?
No. A final method cannot be overridden in any subclass. It can be inherited and called normally, but the subclass cannot provide its own version. Attempting to override a final method causes a compile-time error. This is intentional and is commonly used in security-critical or framework-level code.
What is a final variable in Java?
A final variable is one whose value cannot be changed after it has been assigned. It must be initialised either at the point of declaration or inside a constructor. Static final variables serve as constants in Java. Trying to reassign a final variable anywhere in the code produces a compile error.
What is the difference between final and finally in Java?
final is a modifier used on variables, methods, and classes to prevent change or extension. finally is a block used in exception handling that runs cleanup code regardless of whether an exception occurred. They share no functional relationship; the similar names are coincidental in the language design.
What is effectively final in Java?
A variable is effectively final in Java if it is never reassigned after its initial assignment, even without the final keyword. Java 8 introduced this concept to allow lambda expressions and anonymous classes to use local variables that are not explicitly declared final but behave as if they are.
The one-line answer to what is the final keyword in Java: it is a compile-time lock that stops reassignment, overriding, or subclassing depending on where you use it. Master it and you will write safer, cleaner Java code from day one.
- Open your IDE this week and write a small Java class that uses all three: a final constant, a final method, and a final class. Compile it, then deliberately try to break each rule and read the error messages.
- Review the Oracle Java documentation entry on modifiers to see how
finalinteracts withstaticandabstract. - If you are building toward a career in cybersecurity, ethical hacking, or secure application development, strong Java fundamentals are the foundation. Explore 3.0 University’s CEH v13 AI online course to see how secure coding principles connect to real-world ethical hacking, or browse the full catalogue of 3.0 University online certification courses in Cybersecurity, AI, Blockchain, and Web3.
Last updated: June 2025. Reviewed by the 3University editorial team.