⏱️ 8 min read • Beginner Level • Lesson 18
A final variable in Java is a variable whose value cannot be changed after it has been assigned.
The final keyword is commonly used to create constants and to prevent accidental value modification.
Before learning final variables, you should understand Java variables, Java data types, and Java naming conventions.
A variable declared with the final keyword is called a final variable.
Once a value is assigned to a final variable, it cannot be reassigned.
final variables cannot be reassigned.static final variables are commonly used as constants.The basic syntax of a final variable is:
final dataType variableName = value;
Example:
final int MAX_MARKS = 100;
final double PI = 3.14159;
final String APP_NAME = "ProwessApps";
In this example, MAX_MARKS stores a fixed value.
We can use it in calculations, but we cannot change it later.
public class FinalVariableExample {
public static void main(String[] args) {
final int MAX_MARKS = 100;
int obtainedMarks = 85;
System.out.println("Obtained Marks: " + obtainedMarks);
System.out.println("Maximum Marks: " + MAX_MARKS);
}
}
Output:
Obtained Marks: 85 Maximum Marks: 100
No. Once a final variable gets a value, it cannot be changed. If you try to assign a new value, Java shows a compilation error.
public class FinalReassignError {
public static void main(String[] args) {
final int age = 20;
// age = 25; // Error: cannot assign a value to final variable age
System.out.println(age);
}
}
A final local variable is declared inside a method, constructor, or block. It must be assigned before it is used and cannot be changed after assignment.
public class FinalLocalVariable {
public static void main(String[] args) {
final String courseName = "Java Programming";
System.out.println(courseName);
}
}
Output:
Java Programming
A final instance variable belongs to an object. Once initialized, it cannot be changed for that object. It can be initialized directly or inside a constructor.
public class FinalInstanceVariable {
final int rollNumber;
FinalInstanceVariable(int rollNumber) {
this.rollNumber = rollNumber;
}
void showRollNumber() {
System.out.println("Roll Number: " + rollNumber);
}
public static void main(String[] args) {
FinalInstanceVariable student = new FinalInstanceVariable(101);
student.showRollNumber();
}
}
Output:
Roll Number: 101
A blank final variable is a final variable that is declared without assigning a value immediately. It must be initialized before the constructor finishes.
public class BlankFinalVariable {
final String studentName;
BlankFinalVariable(String name) {
studentName = name;
}
public static void main(String[] args) {
BlankFinalVariable student = new BlankFinalVariable("Ayan");
System.out.println(student.studentName);
}
}
Output:
Ayan
A static final variable belongs to the class and cannot be changed.
It is commonly used to create constants.
public class StaticFinalVariable {
static final double PI = 3.14159;
public static void main(String[] args) {
System.out.println("PI: " + PI);
}
}
Output:
PI: 3.14159
MAX_VALUE, PI, and DEFAULT_LIMIT.
If a reference variable is declared as final, the reference cannot point to another object. However, the data inside the object may still be changed if the object is mutable.
class Student {
String name;
}
public class FinalReferenceVariable {
public static void main(String[] args) {
final Student student = new Student();
student.name = "Ayan";
System.out.println(student.name);
// student = new Student(); // Error: cannot assign a new object to final reference
}
}
Output:
Ayan
| Normal Variable | Final Variable |
|---|---|
| Value can be changed multiple times. | Value can be assigned only once. |
| Used when data may change. | Used when data should remain fixed. |
| No special keyword is required. | Declared using the final keyword. |
Example: int age = 20; |
Example: final int MAX_AGE = 100; |
| Keyword | Meaning |
|---|---|
| final | Value cannot be reassigned. |
| static | Belongs to class. |
| static final | Class-level constant. |
final variables with static variables.final keyword prevents reassignment.static final variables are commonly used as constants.🧠 Test your understanding with a quick quiz
Topic: Final-variables | Language: Java
class Student {
String name;
}
public class Test {
public static void main(String[] args) {
final Student s = new Student();
s.name = "Ayan";
System.out.println(s.name);
}
}public class Test {
static final double PI = 3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}public class Test {
public static void main(String[] args) {
final int marks = 90;
System.out.println(marks);
}
}🎉 Great job! Continue learning Java step by step.