instance needs to be final, or it may be changed by some tricks that
break encapsulation.
On Sat, 2005-08-06 at 15:59 -0400, Mauricio Nuñez wrote:
> Improved version without sync locking:
>
> class SingletonObj
> {
>
> private static SingletonObj instance;
>
> static
> {
> instance=new SingletonObj();
> }
>
> private SingletonObj()
> {
>
> }
>
> public static SingletonObj getInstance() // without sync !
> {
> return instance;
> }
>
> }
>
> El sáb, 06-08-2005 a las 09:14 -0500, David Johnson escribió:
> > {
> > private static final SingletonObj singleton;
> >
> > // optional - use if you want the initialization to occur as class load
> > time
> > // Otherwise, the initialization will occur at first call to
> > getSingleton();
> >
> > // static initialization at load time
> > static {
> > getSingelton ()/
> > }
> >
> >
> > private SingletonObj ()
> > {
> > super ();
> > singleton = this;
> > }
> >
> > public static synchronized SingletonObj getSingleton ()
> > {
> > if ( singleton == null )
> > {
> > new SingletonObj();
> > }
> > return singleton;
> > }
> >
> >
> > }
> >
> >
> > On Fri, 2005-08-05 at 20:11 -0700, Ming Han wrote:
> > > You can use Singleton pattern, but care must be taken.
> > >
> > > For example
> > >
> > > if ( singleton == null )
> > > {
> > > singleton = new SingletonObj();
> > > }
> > >
> > > There might be a case where few thread running concurrently on the null
> > > checking, then multiple singleton object will be created more than one.
> > >
> > > __________________________________________________
> > > Do You Yahoo!?
> > > Tired of spam? Yahoo! Mail has the best spam protection around
> > > http://mail.yahoo.com
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]