A final variable is a variable whose value can be assigned only once. After a final variable receives its value, you cannot assign a different value to that variable.
This makes final useful when a value should remain unchanged after initialization. For example, a tax rate, maximum allowed marks, or a fixed configuration value may need to remain constant throughout a particular part of an application.
Why Do We Need final Variables?
Imagine a school application where the maximum marks for a subject are always 100. If the program accidentally changes that value to 150 somewhere else, calculations and validations could become incorrect.
A final variable helps prevent such accidental reassignment.
final int MAX_MARKS = 100;
After MAX_MARKS receives the value 100, you cannot assign another value to it.
Think of a final variable as a value with a one-time assignment: once the variable is assigned, that variable cannot be assigned again.
Basic Syntax
The basic syntax is:
final dataType variableName = value;
For example:
final int MAX_MARKS = 100; final double PI_VALUE = 3.14159; final String COUNTRY = "India";
By convention, constants are commonly written using uppercase letters with underscores between words. This naming style makes them easy to identify when reading code.
Trying to Change a final Variable
Once a final variable has been initialized, Java does not allow it to be assigned another value.
public class Main {
public static void main(String[] args) {
final int MAX_MARKS = 100;
MAX_MARKS = 150;
}
}
The second assignment causes a compilation error because MAX_MARKS has already been assigned a value.
final Local Variables
A final variable can be declared inside a method just like an ordinary local variable.
public class Main {
public static void main(String[] args) {
final int MAX_MARKS = 100;
System.out.println(MAX_MARKS);
}
}
Here, MAX_MARKS is a final local variable. Its value cannot be changed after initialization.
Blank final Variable
A final variable does not always have to be initialized on the same line where it is declared. A final variable can be declared first and assigned exactly once later, provided Java can determine that it receives a value before it is used.
public class Main {
public static void main(String[] args) {
final int marks;
marks = 100;
System.out.println(marks);
}
}
This is valid because marks receives its first and only assignment before it is used.
However, trying to assign another value afterward is not allowed.
final int marks; marks = 100; marks = 90;
The second assignment causes a compilation error.
final Instance Variables
A final variable can also be an instance variable. This is useful when every object should have a value that cannot be changed after initialization.
public class Student {
final int studentId;
Student(int studentId) {
this.studentId = studentId;
}
public static void main(String[] args) {
Student student = new Student(101);
System.out.println(student.studentId);
}
}
The studentId variable is final. The constructor assigns its value when the object is created, and that variable cannot later be assigned another value.
A final instance variable can be initialized through a constructor. This allows different objects to receive different final values while still preventing reassignment afterward.
final Static Variables
When final and static are used together, they commonly represent a class-level constant.
public class Student {
static final int MAX_MARKS = 100;
public static void main(String[] args) {
System.out.println(Student.MAX_MARKS);
}
}
The variable belongs to the class because it is static, and its value cannot be reassigned because it is final.
This combination is extremely common in Java applications.
public class ApplicationConfig {
static final String APP_NAME = "Student Management System";
static final int MAX_LOGIN_ATTEMPTS = 3;
}
final vs Constant
Beginners often use the words final and constant as if they always mean exactly the same thing. Technically, they are not identical.
The final keyword means that a variable can be assigned only once. A common Java constant is typically a variable declared as static final, making one immutable value available at the class level.
| Declaration | Meaning |
|---|---|
| final int age | Final variable belonging to its particular context |
| final int id | Final instance variable if declared as a class field |
| static final int MAX | Shared class-level value that cannot be reassigned |
final with Primitive Values
For primitive data types, a final variable cannot be assigned a different primitive value after initialization.
final int age = 25; // age = 30; // Compilation error
The variable remains associated with the value 25.
final with Reference Variables
The behavior becomes more interesting when a final variable stores a reference to an object. The final keyword prevents the reference variable from pointing to a different object, but it does not automatically make the referenced object immutable.
public class Student {
String name;
Student(String name) {
this.name = name;
}
public static void main(String[] args) {
final Student student = new Student("Rahul");
student.name = "Anita";
System.out.println(student.name);
}
}
The reference student remains associated with the same Student object, but the object's name field can still change because that field itself is not final.
Important: final on a reference variable prevents reassignment of the reference. It does not automatically make the referenced object immutable.
final Reference Example
final Student student = new Student("Rahul");
// student = new Student("Anita"); // Not allowed
student.name = "Anita"; // Allowed if name is not final
The first reassignment is not allowed because it attempts to make the final reference point to another object. The second statement changes the object's internal data and can be valid when that field is not final.
Why final Improves Code Safety
Using final where appropriate communicates intent to other developers. When someone sees a final variable, they immediately know that the variable is not supposed to be reassigned.
This can reduce accidental changes and make larger codebases easier to reason about.
| Situation | Why final Helps |
|---|---|
| Maximum marks | Prevents accidental reassignment. |
| Application name | Clearly communicates that the value is fixed. |
| Object identifier | Prevents the identifier reference from being reassigned. |
| Configuration value | Makes the intended fixed value explicit. |
Common Beginner Mistakes
- Trying to assign a new value to a final variable.
- Thinking every final variable must be initialized on the same line.
- Assuming final automatically makes an object immutable.
- Confusing final variables with static variables.
- Using final without understanding whether the variable should actually be fixed.
Best Practices
Use final when a variable should not be reassigned after its initial value has been established. For shared class-level constants, static final is commonly appropriate.
public class Product {
static final double TAX_RATE = 18.0;
final int productId;
Product(int productId) {
this.productId = productId;
}
}
Use meaningful names for constants. Names such as MAX_MARKS, TAX_RATE, and MAX_LOGIN_ATTEMPTS clearly communicate their purpose.
Interview Insight
If an interviewer asks, "What is the final keyword in Java?", explain that final can be applied to variables, methods, and classes, and its effect depends on where it is used. For a variable, final means that the variable can be assigned only once.
If asked whether a final reference makes an object immutable, the answer is no. It prevents the reference from being reassigned, but the object's internal state may still change unless the object itself is designed to be immutable.
Quick Revision
| Concept | Key Point |
|---|---|
| final Variable | A variable that can be assigned only once. |
| Final Local Variable | Can receive one value inside a method or block. |
| Final Instance Variable | Object-level variable that cannot be reassigned after initialization. |
| static final | Commonly used for shared class-level constants. |
| Final Reference | The reference cannot point to another object, but the referenced object may still be mutable. |
| Constant | Commonly represented by a static final class-level variable. |
The final keyword is a simple but powerful tool for expressing intent in Java. It tells both the compiler and other developers that a particular variable should not be reassigned. Once you understand the difference between a final variable and an immutable object, you are ready to explore how Java controls where variables can be accessed through variable scope.
