Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
Created May 24, 2012 19:26
Show Gist options
  • Save alexjlockwood/2783706 to your computer and use it in GitHub Desktop.
Save alexjlockwood/2783706 to your computer and use it in GitHub Desktop.
newInstance fragment instantiation #1
public static class Singleton {
private static final Singleton instance = null;
/**
* Make the class private to prevent direct instantiation.
* this forces clients to call newInstance(), which will
* ensure the class' Singleton property.
*/
private Singleton() { }
public static Singleton newInstance() {
/**
* If instance is null, then instantiate the object
* by calling the default constructor (this is OK
* since we are calling it from within the class).
*/
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment