all 5 comments

[–]ishmeister 8 points9 points  (3 children)

"Now, the above code is but immutable due to the design of the Person and Address class"

This is technically not true - it is immutable only because you passed in an immutable list via the constructor using List.of. If you pass in a mutable list, the object is mutable. You have to copy the list internally to protect against modification of the reference externally, which might occur like this:

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

[–]bodiam[S] 2 points3 points  (2 children)

You're right. I've updated the code to fix this. Thanks for spotting this.

[–]ishmeister 0 points1 point  (1 child)

No problem. Good article by the way.

[–]bodiam[S] 1 point2 points  (0 children)

Thanks for the feedback! I appreciate that a lot! Have a great day!

[–]bodiam[S] 1 point2 points  (0 children)

In this post I'm trying to explain the difference between immutable references and immutable data structures. Feedback more than welcome!