Skip to content

Instantly share code, notes, and snippets.

@jon-ruckwood
Created April 25, 2020 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jon-ruckwood/3e4f0bddea748fe2ab95d6e6a515d246 to your computer and use it in GitHub Desktop.
Save jon-ruckwood/3e4f0bddea748fe2ab95d6e6a515d246 to your computer and use it in GitHub Desktop.
Compile with `--enable-preview`
public class Java14RecordExample {
public static void main(String[] args) {
var p = new Person("Boz", "Scaggs");
System.out.println("toString(): " + p);
System.out.println("---");
System.out.println("Howdy " + p.name() + "!");
}
}
record Person(String firstName, String lastName) {
public Person {
requireNonNull(firstName, "firstName is required");
requireNonNull(lastName, "lastName is required");
}
public String name() {
return firstName + " " + lastName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment