June 2018
Intermediate to advanced
310 pages
6h 32m
English
In Java, we are used to the concept of getters and setters. A typical class may look something like this:
public class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } // More methods come here}
If we want to get a person's name, we call getName(). If we want to change it, we call setName(). That's quite simple.
If we want to set the name only once, during object instantiation, we can specify the non-default constructor and remove the setter as follows:
public class ImmutablePerson { private String name; public ImmutablePerson(String name) { this.name = name; } public String getName() { return name; }}
All this dates back to the beginning of Java, somewhere ...
Read now
Unlock full access