final vs Immutability
You'll learn the crucial difference between preventing reassignment and preventing object modification.
When you declare a variable final, you're telling Java: "This variable can only point to one thing—it cannot be reassigned." But here's the catch: if that variable references an object, final doesn't stop you from changing the object's internal state.
Think of final like a keychain. Once you attach a key to the ring, you can't swap it for a different key—that's what final prevents. But you can still use that key to unlock something and change what's inside the room. The keychain is locked, but the door isn't.
The Key Distinction
final int number = 10;
// number = 20; // ❌ Compilation error: cannot reassign final variable
This works as expected—int is a primitive type, so final makes it truly unchangeable.
But with objects (which you haven't learned yet, but this is important to understand conceptually):
final int[] numbers = {1, 2, 3};
numbers[0] = 99; // ✅ Allowed! We're changing the array's contents
// numbers = new int[]{4, 5, 6}; // ❌ Cannot reassign the reference
The final keyword only prevents reassigning the reference (the arrow pointing to the object). It doesn't prevent modifying the object's internal data.
Immutability means the object's state truly cannot change after creation—no methods exist to modify it, and all its fields are unchangeable. This is a property of how the object is designed, not something final provides by itself.
Key Takeaway: final prevents reassignment of a variable; it does NOT make objects immutable—those are two separate concepts that work together but aren't the same thing.