How it works...

If you want to implement a class that stores the first and last name of a person, you would normally implement something like this:

    public class PersonMutable {       private String firstName;       private String lastName;       private Date birthDate;        public String getFirstName() {         return firstName;       }        public void setFirstName(String firstName) {         this.firstName = firstName;       }        public String getLastName() {         return lastName;       }        public void setLastName(String lastName) {         this.lastName = lastName;       }       public Date getBirthDate() {         return birthDate;       }        public void setBirthDate(Date birthDate) {         this.birthDate = birthDate;       }      }

You can convert this class into an immutable class by following the rules explained earlier. The ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.