这个静态内部类实现的单例是迟加载且线程安全的吗?
public class JiveProperties { private static final String LOAD_PROPERTIES = "SELECT name, propValue FROM ofProperty"; private JiveProperties() { System.out.println("JiveProperties constructor"); } static { System.out.println("static block"); } private static class JivePropertyHolder { static { System.out.println("initing1"); } private static final String LOAD_PROPERTIES = "Inner Static Class..."; private JivePropertyHolder() { System.out.println("inner class..."); } private static final JiveProperties instance = new JiveProperties(); static { System.out.println("initing2"); } } public static JiveProperties getInstance() { return JivePropertyHolder.instance; } public static void main(String[] args) { System.out.println(JiveProperties.JivePropertyHolder.LOAD_PROPERTIES);} }
?
结果:
static block
Inner Static Class...
?
因此初步判断是迟加载且是线程安全的(由final保证)
?