Java – Final
The final keyword in Java is a non-access modifier used to place a restriction on a variable, method, or Class. The exact restriction depends on where final is used: a final variable cannot be reassigned, a final method cannot be overridden, and a final class cannot be extended.
In this tutorial, we shall learn about the final keyword, and its usage, with the help of illustrative example programs. We will also cover common points that beginners often confuse, such as final vs static final, blank final variables, final reference variables, and when to use final in Java programs.
Final keyword in Java
Final keyword in Java can be applied in the following scenarios:
- Final Variable – Once a variable is declared as final, it must be initialized exactly once. After initialization, the variable cannot be reassigned. A final instance variable may be initialized during declaration or inside every constructor.
- Final Method – Once a method is declared as final, it can be inherited but cannot be overridden by any subclass.
- Final Class – Once a class is declared as final, it cannot be inherited by another class.

Where the final keyword is used in Java
| Use of final | What Java restricts | Example |
|---|---|---|
| Final variable | The variable cannot be reassigned after it is initialized. | final int speedLimit = 80; |
| Final instance variable | Each object gets one value that cannot be changed after construction. | public final String EngineNumber; |
| Static final variable | The class has one constant value shared by all objects. | static final double PI = 3.14159; |
| Final method | Subclasses cannot override the method. | public final void accelerate() |
| Final class | No class can extend it. | public final class AudiR8 |
Final Variable in Java
Final variable once initialized can never be modified.
Following Example Java program, Audi.java, demonstrates following two scenarios :
- BRAND is a final variable and initialized to “AUDI” during declaration itself and can never be changed. They remain constant for all the objects of class type Audi.
- EngineNumber is only declared as final but not initialized. These kind of variables could be initialized in Constructor. They remain constant only for the object of Audi, i.e., each Audi object can have different EngineNumber.
Audi.java
public class Audi {
public final String BRAND = "AUDI";
public final String EngineNumber;
public Audi(String EngineNumber){
this.EngineNumber = EngineNumber;
}
public static void main(String[] args) {
Audi audi = new Audi("ABCD1234CDEF4567");
System.out.println("Engine Number : "+audi.EngineNumber);
System.out.println("Car Brand : "+audi.BRAND);
}
}
Output
Engine Number : ABCD1234CDEF4567
Car Brand : AUDI
In this example, BRAND is initialized directly in the field declaration. EngineNumber is a blank final variable, which means it is declared as final but assigned later. Since it is an instance field, Java requires it to be assigned before the constructor finishes.
Now we shall try to modify the final variable and understand what happens.
Audi.java
public class Audi {
public final String EngineNumber;
public Audi(String EngineNumber){
this.EngineNumber = EngineNumber;
}
public static void main(String[] args) {
Audi audi = new Audi("ABCD1234CDEF4567");
audi.EngineNumber = "ERTY1234CDEF4568";
System.out.println("Engine Number : "+audi.EngineNumber);
}
}
Java Compiler throws an Error.
The final field Audi.EngineNumber cannot be assigned.
Final reference variable in Java does not make the object immutable
A common misunderstanding is that a final reference variable makes the object itself unchangeable. In Java, final prevents reassignment of the reference. If the referenced object is mutable, its internal state can still change.
import java.util.ArrayList;
import java.util.List;
public class FinalReferenceExample {
public static void main(String[] args) {
final List<String> brands = new ArrayList<>();
brands.add("Audi");
brands.add("BMW");
System.out.println(brands);
// Not allowed:
// brands = new ArrayList<>();
}
}
[Audi, BMW]
The brands reference cannot be assigned to a different list, but elements can still be added to the same list. To make object state unchangeable, use immutable classes, private fields, defensive copies, and unmodifiable collections where appropriate.
Static final variable in Java for constants
A variable declared with both static and final is commonly used as a constant. static means the value belongs to the class rather than each object. final means the value cannot be reassigned after initialization.
public class Circle {
public static final double PI = 3.14159;
public static double area(double radius) {
return PI * radius * radius;
}
public static void main(String[] args) {
System.out.println(area(5));
}
}
78.53975
By convention, constant names in Java are written in uppercase letters with underscores between words, for example MAX_SPEED or DEFAULT_TIMEOUT.
Blank final variable rules in Java constructors
A blank final variable is a final variable that is not initialized at the point of declaration. For an instance field, every constructor must assign it exactly once. If any constructor path leaves it unassigned, the code does not compile.
class Vehicle {
private final String registrationNumber;
Vehicle(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
}
The same rule applies to blank static final variables, except they must be initialized in a static initializer block or directly in the declaration.
class AppConfig {
public static final String APP_NAME;
static {
APP_NAME = "TutorialKart Java App";
}
}
Final Method in Java
Final method cannot be overridden. This property could also be used to keep restrictions in Polymorphism – Method Overriding.
In the following example, we have two classes: Car.java and Audi.java. Audi class inherits Car class, and accelerate() method of car is declared final.
Car.java
public class Car {
public void brake(){
System.out.println("break in Car");
}
public final void accelerate(){
System.out.println("accelerate in Car");
}
}
Audi.java
public class Audi extends Car {
public static void main(String[] args) {
Audi audi = new Audi();
audi.accelerate();
audi.brake();
}
}
Output
accelerate in Car
break in Car
If we try to override accelerate() method in Audi.java, we will get Error.
Audi.java
public class Audi extends Car {
public static void main(String[] args) {
Audi audi = new Audi();
audi.accelerate();
audi.brake();
}
public void accelerate(){
System.out.println("accelerate in Audi");
}
}
Java Compilation Error occurs
Cannot override the final method from Car
A final method is still inherited. In the first Audi.java example, the object calls accelerate() successfully because it inherits the method from Car. The restriction applies only when a subclass tries to provide a new implementation with the same method signature.
Final Class in Java
Final Class can never be inherited by other classes.
AudiR8.java
public final class AudiR8 {
public final String BRAND = "AUDI";
public final String EngineNumber;
public AudiR8(String EngineNumber){
this.EngineNumber = EngineNumber;
}
}
We shall now try to extend this AudiR8.java from another class and understand what happens
AnotherCar.java
public class AnotherCar extends AudiR8 { }
Java Compiler throws the following error.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at AnotherCar.main(AnotherCar.java:4)
Depending on the compiler or IDE, the message may be shown as a compile-time error such as cannot inherit from final AudiR8. The meaning is the same: a final class cannot be used as a parent class.
Final local variables and final method parameters in Java
The final keyword can also be used with local variables and method parameters. A final local variable or final parameter cannot be reassigned inside the method.
public class FinalParameterExample {
public static void printDiscount(final int discount) {
System.out.println("Discount: " + discount);
// Not allowed:
// discount = 20;
}
public static void main(String[] args) {
final int price = 1000;
printDiscount(10);
// Not allowed:
// price = 1200;
}
}
Discount: 10
Final local variables are useful when a value should not be reassigned after calculation. They also make the programmer’s intention clear to anyone reading the code.
When should we use final keyword in Java?
- Use final variables for values that must not be reassigned after initialization.
- Use static final variables for constants shared by all objects of a class.
- Use final methods when a method’s behavior must remain unchanged in subclasses.
- Use final classes when the class is not designed for inheritance.
- Use final parameters or final local variables when reassignment inside a method would make the code harder to read or unsafe.
Do not use final everywhere without a reason. It is most helpful when it communicates a meaningful rule in the program: this value should not be reassigned, this method should not be overridden, or this class should not be extended.
Common mistakes with final keyword in Java
- Assuming final means immutable: A final reference cannot point to a different object, but the object may still be mutable.
- Leaving a blank final field unassigned: Every constructor must assign a blank final instance field exactly once.
- Trying to override a final method: A subclass can call the inherited final method but cannot override it.
- Trying to extend a final class: A final class cannot be used with the extends keyword.
- Confusing final with static: final prevents reassignment; static makes the member belong to the class.
Editorial QA checklist for Java final keyword examples
- Confirm that every final variable shown in an example is initialized exactly once.
- Check that final instance fields are initialized in all constructors or at the field declaration.
- Do not describe a final reference variable as an immutable object unless the object’s class is actually immutable.
- Verify that final method examples explain inheritance separately from overriding.
- Check that final class examples show a compile-time restriction, not a runtime design choice.
Official Java reference for final classes and methods
For the official Java tutorial explanation, refer to Oracle’s page on final methods and classes. It explains how final prevents method overriding and class inheritance in Java.
FAQs on final keyword in Java
Is final a valid Java keyword?
Yes. final is a valid Java keyword. It is used with variables, methods, and classes to apply different restrictions.
What is the difference between static and final keyword in Java?
static means a member belongs to the class rather than to each object. final means the variable cannot be reassigned, the method cannot be overridden, or the class cannot be extended. They are often used together for constants, as in public static final int MAX_SIZE = 100;.
Can a final variable be initialized later in Java?
Yes, a blank final variable can be initialized later, but only once. A final instance field can be assigned in a constructor. A final local variable can be assigned before it is used, as long as it is assigned only one time.
Can a final method be overloaded in Java?
Yes. A final method cannot be overridden with the same signature in a subclass, but it can be overloaded by creating another method with the same name and different parameters.
Does final make an object immutable in Java?
No. Final makes the reference variable non-reassignable. If the object itself is mutable, its fields or contents may still change through its methods.
Conclusion
In this Java Tutorial, we have learned about final keyword in Java that can be applied to a Variable, Method or Class and also the restrictions Java keeps on those, that are declared final.
In our next tutorial, we shall learn about another Object Oriented Concept – Abstraction in Java.
TutorialKart.com