Created
February 14, 2020 09:22
-
-
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(...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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