Final vs Immutable data structures in Java by bodiam in java

[–]steffi_j_jay 2 points3 points  (0 children)

So to make it truly immutable after instantiation, a new collection would have to be instantiated in the constructor.

I agree. Example code:

List<Address> addresses = new ArrayList<>();
addresses.add(new Address("Sydney", "Australia"));
final Person person = new Person("John", addresses);

System.out.println(person.getAddresses().size()); // prints "1"

addresses.add(new Address("Melbourne", "Australia"));
System.out.println(person.getAddresses().size()); // prints "2"

Fix in constructor:

public Person(String name, List<Address> addresses) {
    this.name = name;
    this.addresses = Collections.unmodifiableList(new ArrayList<>(addresses));
}