March 2019
Intermediate to advanced
242 pages
6h 21m
English
Although it is allowed, before adding variables or fields to a data class, ask yourself, Are the fields derived from the state? Fields that are not derived from the state pose a serious violation of the initial concept of the data classes. The following code is an example that defines an additional field, style, derived from the state of the Emp data class:
record Emp(String name, int age) {
private String style;
Emp(String name, int age) {
//.. initialize name and age
if (age => 15 && age =< 30) style = "COOL";
else if (age >= 31 && age <= 50) style = "SAFE";
else if (age >= 51) style = "ELEGANT";
}
public String getStyle() {
return style;
}
}
The preceding code works well because the state of the Emp data class ...
Read now
Unlock full access