java - Why a static singleton is a simple and elegant solution to avoid 'DCL'? -


when need singleton, static field elegant solution?

class helpersingleton {   static helper singleton = new helper();    public static helper getinstance() {        return singleton;      }   } 

when 2 threads access getinstance @ same time, there chance field singleton not initialized completely? or see default values fields of helper object, rather values set in constructor? static singleton lazy initialization?

i mean, static helper singleton = new helper(); assigment atomic? , won't return default values ?

1) when thread accesses static getinstance first time class loader has load helpersingleton class , lock other thread before class loaded. there implicit synchronization present. j.bloch "effective java" item 71 modern vm synchronize field access initialize class. once class initialized, vm patch code subsequent access field not involve testing or synchronization.

2) singleon lazy, because there 1 access point - getinstance. not instance created on demand whole class loaded on demand. class not initialized until used [jls, 12.4.1].


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -