The record class
The record
class was added to the SDK in Java 16. It was a long-awaited Java feature. It allows you to avoid writing boilerplate code in a case when you need an immutable class (with getters only), which looks similar to the following Person
class (see the Record
class in the ch02_oop
folder):
final class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int age() { return age; }
public String name() { return name; }
@Override
public boolean equals(Object o) {
//implementation not shown for brevity
...