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

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -