In the previous chapter, we created a mutable class named VirtualCreature
. We provided setter methods to change the values for the hat
, visibilityLevel
, and birthYear
fields. We were able to change the birthYear
by calling the setAge
method.
Virtual creatures change their age, hat, and visibility level after they evolve. When they evolve, they become a different creature, and therefore, it would make sense to generate a new instance after this evolution happens. Thus, we will create the immutable version of the VirtualCreature
class and we will call it ImmutableVirtualCreature
.
The following lines show the code for the new ImmutableVirtualCreature
class. The code file for the sample is included in the java_9_oop_chapter_05_01
folder, in the example05_06.java
file.
import java.time.Year; public class ImmutableVirtualCreature { public final String name; public final int birthYear; public final String hat; public final...