InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
SKM
Revision Mode
Document technical questions and best-practice answers.
Can you explain what is immutable class?
An immutable class is a class whose objects cannot be changed after they are created.
Key Points:
1: Once you create an object of an immutable class, you cannot modify its values.
2: All fields (variables) in the class are usually marked as final and private.
3: There are no setter methods—only getter methods to access the values.
Example:
final class Student {
private final String name;
private final int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Here, once a Student object is created, you cannot change its name or age.
In simple words:
Immutable means "unchangeable." Just like your birth date, once set, it cannot be changed!
Ready for commit