OO Programming with Java


enumeration and records

  1. Java also integrates the concept of "record" illustrated below:
public record Item(Kind kind, String name, Double price) {
  public String toString() { return name+" ("+price+" - "+kind+")";}
}

This code is a "syntactic sugar" (i.e. "is equivalent to" or "can be transformed into"):

public class Item {
  private Kind kind;
  private String name;
  private Double price;
  // + constructor + accessors + equals + ...

  public String toString() { return name+" ("+price+" - "+kind+")";}
}