Initialization-on-demand Holder Idiom - Example Java Implementation

Example Java Implementation

This implementation is a well-performing and concurrent implementation valid in all versions of Java. The original implementation from Bill Pugh (see links below), based on the earlier work of Steve Quirk, has been modified to reduce the scope of LazyHolder.INSTANCE to private and to make the field final.

public class Something { private Something { } private static class LazyHolder { private static final Something INSTANCE = new Something; } public static Something getInstance { return LazyHolder.INSTANCE; } }

Read more about this topic:  Initialization-on-demand Holder Idiom