diff --git a/src/main/java/org/joda/time/convert/ConverterManager.java b/src/main/java/org/joda/time/convert/ConverterManager.java index 2195a3726..e747986e3 100644 --- a/src/main/java/org/joda/time/convert/ConverterManager.java +++ b/src/main/java/org/joda/time/convert/ConverterManager.java @@ -81,15 +81,14 @@ public final class ConverterManager { /** - * Singleton instance, lazily loaded to avoid class loading. + * Holds the singleton instance, lazily loaded to avoid class loading. */ - private static ConverterManager INSTANCE; + private static final class LazyConverterManagerHolder { + static final ConverterManager INSTANCE = new ConverterManager(); + } public static ConverterManager getInstance() { - if (INSTANCE == null) { - INSTANCE = new ConverterManager(); - } - return INSTANCE; + return LazyConverterManagerHolder.INSTANCE; } private ConverterSet iInstantConverters; diff --git a/src/test/java/org/joda/time/convert/TestConverterManager.java b/src/test/java/org/joda/time/convert/TestConverterManager.java index 06926f641..67e2c83f5 100644 --- a/src/test/java/org/joda/time/convert/TestConverterManager.java +++ b/src/test/java/org/joda/time/convert/TestConverterManager.java @@ -127,9 +127,12 @@ public void testSingleton() throws Exception { Constructor con = cls.getDeclaredConstructor((Class[]) null); assertEquals(1, cls.getDeclaredConstructors().length); assertEquals(true, Modifier.isProtected(con.getModifiers())); + + Class holderCls = Class.forName(cls.getName() + "$LazyConverterManagerHolder"); + assertEquals(true, Modifier.isPrivate(holderCls.getModifiers())); - Field fld = cls.getDeclaredField("INSTANCE"); - assertEquals(true, Modifier.isPrivate(fld.getModifiers())); + Field fld = holderCls.getDeclaredField("INSTANCE"); + assertEquals(true, Modifier.isFinal(fld.getModifiers())); } //-----------------------------------------------------------------------