Skip to content

Instantly share code, notes, and snippets.

@scottashipp
Created November 18, 2014 00:21
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 scottashipp/ec9f1c82fc7da24919cc to your computer and use it in GitHub Desktop.
Save scottashipp/ec9f1c82fc7da24919cc to your computer and use it in GitHub Desktop.
Example of using static factory methods in Java
public class Person {
static int NEXT_ID = 0;
private String first;
private String last;
private int id;
private Person() {} //prevents instantiation not using "create"
private Person(String f, String l, int i) {
first = f;
last = l;
id = i;
}
public static Person create(String f, String l) {
return new Person(f, l, NEXT_ID++);
}
public String getName() {
return String.format("%s %s", first, last);
}
public int getId() {
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment