Posts

Showing posts from June, 2025

Why final in Java Doesn't Always Mean "Immutable" - Explained for Beginners

Image
When you hear the word   final   in Java, what comes to mind? Most developers immediately think it means you can’t change the variable . And while that’s partially true, the reality is more nuanced — and it can trip you up in interviews and real-world coding alike. In this blog, let’s explore what final really means in Java — and more importantly, what it doesn’t mean. ☕ What Does final Do in Java? When you declare a variable with the final keyword in Java, you’re saying:      “This variable can’t be assigned to a new value after it’s been initialized.” This applies to: Primitive types like int , boolean , etc. Object references like ArrayList , HashMap , etc. But here’s the key twist: Final only protects the reference , not the actual object it points to. 🔍 Example 1: Primitive Types final int x = 10 ; x = 20 ; // ❌ Compilation error Here, trying to change x gives a compiler error — exactly what you'd expect. 🔍 Example 2: Object References final...