Skip to content

Instantly share code, notes, and snippets.

@poetix
Last active December 6, 2017 14:27
Show Gist options
  • Save poetix/6627a3a33216098cefbae2e72af5bf99 to your computer and use it in GitHub Desktop.
Save poetix/6627a3a33216098cefbae2e72af5bf99 to your computer and use it in GitHub Desktop.
public final class Person {
// Publicly accessible, all parameters validated.
public static Person with(String name, int age, String favouriteColour) {
checkArgument(age > 0, "age must be greater than 0");
return new Person(
checkNotNull(name, "name must not be null"),
age,
checkNotNull(favouriteColour, "favouriteColour must not be null")
);
)
private final String name;
private final int age;
private final String favouriteColour;
// Private, does nothing but assign parameters to fields
private Person(String name, int age, String favouriteColour) {
this.name = name;
this.age = age;
this.favouriteColour = favouriteColour;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment