Return to site

JAVA 14: Record feature example

It increases considerably the readability

· java

Before

public class Location {
 private String postCode;
 private String country;
 private String countryAbbreviation;
 private List<Place> places;

 @JsonProperty("post code")
 public String getPostCode(){
  return postCode;
 }
 
 @JsonProperty("post code")
 public void setPostCode(){
  this.postCode = postCode;
 }

 public String getCountry(){
  return country;
 }
 
 public void setCountry(){
  this.country = country;
 }

 @JsonProperty("country abbreviation")
 public String getCountryAbbreviation(){
  return countryAbbreviation;
 }
 
 @JsonProperty("country Abbreviation")
 public void setCountryAbbreviation(){
  this.countryAbbreviation= countryAbbreviation;
 }

 public List<Place> getPlaces(){
  return places;
 }

 public void setPlaces(){
  this.places = places;
 }

 @Override
 public boolean equals(Location location){
  // a bunch of code
 }

 @Override
 public String toString(){
  // a bunch of code
 }
}

After

public record Location (
 @JsonProperty("post code") String postCode,
 @JsonProperty("country") String country,
 @JsonProperty("country abbreviation") String countryAbbreviation,
 @JsonProperty("places") List<Place> places){}