Java final with objects: A simple explanation

Java Concept Explained Simply: final with Objects Many developers get confused about what happens when we use final with objects in Java. Let’s break it down with a simple example class Person { String name = "Alex"; } public class Test { public static void main(String[] args) { final Person p = new Person(); p.name = "Sam"; System.out.println(p.name); } } Explanation: The keyword final means you cannot reassign the reference p to another object. ✅ p = new Person(); → ❌ Compilation Error But you can modify the internal state of the object that p is pointing to. ✅ Changing p.name is perfectly allowed. So, when we run this program: 👉 Output: Sam #Java #CodingConcepts #InterviewPreparation #JavaDeveloper #LearnWithExample

To view or add a comment, sign in

Explore content categories