Final Variables
Final Variables
A school program needs reliable data and clear rules. A final variable can be assigned only once. With an object reference, the restriction applies to the reference assignment, not automatically to the object’s contents.
Understand
A final variable can be assigned only once.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Final does not make a referenced object immutable. Its mutable fields may still change.
Final Variables
In Java, the keyword final is used to mark a variable as unchangeable once it's been assigned. But there's more to it than just being a constant. Understanding final goes beyond syntax—it's about design intent and program safety.
What is a final variable?
A final variable in Java is a variable whose value cannot be changed once it is assigned. This makes it a constant. You can think of it like a locked box—you can put something in it once, but never change it afterward.
Declaring final variables
You can declare a final variable at any of the following levels:
- Local variable – inside a method
- Instance variable – inside a class but outside any method
- Static variable – class-level constant
public class FinalVariableExample {
final int instanceVariable = 10; // instance-level final
static final int STATIC_CONSTANT = 100; // class-level final
void show() {
final int localVar = 5; // local final
System.out.println("Local final variable: " + localVar);
}
}
Assigning final variables
Final variables must be assigned exactly once. This can happen:
- At the time of declaration
- Inside a constructor (for instance variables)
public class FinalAssignment {
final int value;
FinalAssignment() {
value = 42; // allowed: assigned in constructor
}
void display() {
System.out.println("Final value: " + value);
}
}
Final value: 42
What happens if you try to reassign a final variable?
Java will throw a compile-time error if you try to assign a value to a final variable more than once.
public class FinalError {
public static void main(String[] args) {
final int number = 10;
number = 20; // Compilation Error: cannot assign a value to final variable
}
}
Final reference variables
When you make an object reference final, you cannot reassign the reference to another object. However, you can still modify the internal state of the object if it's mutable.
class Book {
String title = "Java Basics";
}
public class FinalReference {
public static void main(String[] args) {
final Book book = new Book();
book.title = "Advanced Java"; // Allowed: changing state
// book = new Book(); // Not allowed: reassigning reference
System.out.println(book.title);
}
}
Advanced Java
Using final with static variables (constants)
Often, final is combined with static to define constants.
public class MathConstants {
public static final double PI = 3.14159;
public static final int MAX_USERS = 100;
}
You access these constants using the class name:
System.out.println(MathConstants.PI); // 3.14159
Best practices
- Use
finalto communicate intent — that the value should not change. - Combine
staticandfinalto define true constants. - Use final for local variables in methods to avoid accidental changes.
- Declare method parameters as final if they shouldn’t be reassigned inside the method.
Final vs Constant
A variable declared final is not necessarily a constant unless it is also static. For instance-level final variables, the value is fixed per object, not for the class.
Conclusion
The final keyword in Java is more than just a restriction—it's a promise. It tells the compiler, and future developers, that the variable is safe from change.
Important boundaries and mistakes
Final does not make a referenced object immutable. Its mutable fields may still change.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Final does not make a referenced object immutable.
Java 17 · Main.java · complete practice program
public class Main {
interface Named {
String label();
}
static class Student implements Named {
static String school = "River School";
String name;
Student(String name) {
this.name = name;
}
public String label() {
return name;
}
}
public static void main(String[] args) {
final Student s = new Student("Diya");
s.name = "Aarav";
System.out.println(s.name);
}
}
Now change one thing: Try assigning s to another new Student. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
interface Named {
String label();
}
static class Student implements Named {
static String school = "River School";
String name;
Student(String name) {
this.name = name;
}
public String label() {
return name;
}
}
public static void main(String[] args) {
final Student s = new Student("Diya");
s.name = "Aarav";
System.out.println(s.name);
}
}
Expected output
AaravA final variable can be assigned only once. With an object reference, the restriction applies to the reference assignment, not automatically to the object’s contents.
Final does not make a referenced object immutable. Its mutable fields may still change.
Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.
Worked variation: Try assigning s to another new Student. That reassignment does not compile; changing its name field still does.
Check your understanding
Check a common mistake
What to remember
A final variable can be assigned only once. With an object reference, the restriction applies to the reference assignment, not automatically to the object’s contents. Final does not make a referenced object immutable. Its mutable fields may still change.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.