Record type a new feature in Java 14.

By

 The records are new-type declarations in Java 14. It simplifies and reduces boilerplate code that simply acts as "data carrier" i.e. POJO or domain class.


POJO or Domain Class:


public class Employee {

private String name;

private Integer age;


public Employee(String name, Integer age) {

this.name = name;

this.age = age;

}


public String getName() {

return name;

}


public void setName(String name) {

this.name = name;

}


public Integer getAge() {

return age;

}


public void setAge(Integer age) {

this.age = age;

}

}



With record type:


record Employee (String name, Integer age) {}


Record is responsible to generate setters, getters, medhods, constructor, equals(), hashCode() and toString() implicitly.

0 comments:

Post a Comment