immutability - Make immutable Java object -
my goal make java object immutable. have class student
. coded in following way achieve immutability:
public final class student { private string name; private string age; public student(string name, string age) { this.name = name; this.age = age; } public string getname() { return name; } public string getage() { return age; } }
my question is, best way achieve immutability student
class?
your class not immutable strictly speaking, immutable. make immutable, need use final
:
private final string name; private final string age;
although difference might seem subtle, it can make significant difference in multi-threaded context. immutable class inherently thread-safe, immutable class thread safe if safely published.
Comments
Post a Comment