- 1Java OOP Introduction
- 2Java Class
- 3Java Class Constructor
- 4Java Class Objects
- 5Java Access Modifiers
- 6Java Static Variables in Classes
- 7Java Static Methods Explained
- 8Java Static Blocks
- 9Java final Variables
- 10Java final Methods
- 11Java final class
- 12Inheritance in Java
- 13Java Method Overriding
- 14Java Abstraction in OOP
- 15Interfaces in Java
- 16Polymorphism in Java
- 17Encapsulation in Java
- 18Java Nested Classes
- 19Java Nested Static Class
- 20Java Anonymous Class
- 21Java Singleton Class
- 22Java Enums
- 23Reflection in Java
Java final Variables
Explaination and Examples
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
final
to communicate intent — that the value should not change. - Combine
static
andfinal
to 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.
QUIZ
Question 1:What is the key characteristic of a final
variable in Java?
Question 2:Final instance variables can be assigned within a constructor.
Question 3:Which of the following are valid ways to assign a final
variable in Java?
Question 4:Consider the code snippet:
final int speed = 60;
speed = 80;
What will happen at compile time?
final int speed = 60;
speed = 80;