March 2019
Intermediate to advanced
242 pages
6h 21m
English
The compiler generates a default constructor for a data class, along with accessor methods and the default implementation of the methods from the object class. A developer can overload the constructors and add more methods to a data class, as follows:
record Emp(String name, int age) {
// overloading constructor
public Emp(String name, String style) {
this.name = name;
if (style.equals("COOL") age = 20;
else if (style.equals("SAFE") age = 30;
else if (style.equals("ELEGANT") age = 50;
else age = 70;
}
}
public String fancyOutput() { // additional method
return "My style is COOL";
}
}
Read now
Unlock full access