Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Created February 14, 2020 09:22
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 thomasdarimont/02c026e8cebf03ec88ff1314d97da68b to your computer and use it in GitHub Desktop.
Save thomasdarimont/02c026e8cebf03ec88ff1314d97da68b to your computer and use it in GitHub Desktop.
Example for using static factory methods with Records to ease using DSLs - you can use Record(...) instead of new Record(...)
package demo;
import java.util.Objects;
import static demo.Recordz.Person.Person;
public class Recordz {
record Person(String firstname, String lastname) {
public Person {
this.firstname = Objects.requireNonNull(firstname, "firstname");
this.lastname = Objects.requireNonNull(lastname, "lastname");
}
public static Person Person(String firstname, String lastname) {
return new Person(firstname, lastname);
}
}
public static void main(String[] args) {
System.out.println(new Person("Theo", "Tester"));
System.out.println(Person("Theo", "Tester"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment