Created
April 10, 2015 13:33
-
-
Save dmikurube/13354d388490380d5230 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
main() 1
create()
static{}
get()
main() 2