⬅ Previous Topic
Java Static BlocksNext Topic ⮕
Java final Methods⬅ Previous Topic
Java Static BlocksNext Topic ⮕
Java final MethodsIn 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.
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.
You can declare a final variable at any of the following levels:
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);
}
}
Final variables must be assigned exactly once. This can happen:
public class FinalAssignment {
final int value;
FinalAssignment() {
value = 42; // allowed: assigned in constructor
}
void display() {
System.out.println("Final value: " + value);
}
}
Final value: 42
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
}
}
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
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
final
to communicate intent — that the value should not change.static
and final
to define true constants.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.
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.
final
variable in Java?final
variable in Java?final int speed = 60;
speed = 80;
What will happen at compile time?⬅ Previous Topic
Java Static BlocksNext Topic ⮕
Java final MethodsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.