Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created April 10, 2015 13:33
Show Gist options
  • Save dmikurube/13354d388490380d5230 to your computer and use it in GitHub Desktop.
Save dmikurube/13354d388490380d5230 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
public class InitializationOnDemand {
private static class LazyHolder {
private static final Map<String, String> instance = create();
static {
System.out.println("static{}");
instance.put("foo", "bar");
}
private static Map<String, String> create() {
System.out.println("create()");
return new HashMap<String, String>();
}
public static Map<String, String> get() {
System.out.println("get()");
return instance;
}
}
public static void main(String[] args) {
System.out.println("main() 1");
Map<String, String> instance = LazyHolder.get();
System.out.println("main() 2");
}
}
@dmikurube
Copy link
Author

main() 1
create()
static{}
get()
main() 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment