Oh yes, I agree. 

private static final Foo instance = new Foo();

rules!

Except where new Foo() could cause exceptions, then I would go for lazy initialization.

That said, I would also prefer no Singletons at all ....
Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 


Mike Morris wrote:
Dr Heinz M. Kabutz wrote:

  
Your second solution is great - if you don't mind the instance being
created the first time the class is loaded.
    

So you're saying that there are tradeoffs...  what's new?

Personally, unless the initialisation is AMAZINGLY expensive (and I'd 
want to see hard numbers to prove it! ;-) or time-consuming I see no 
point in lazy init in relation to singletons.

I mean, what's really the cost of

     private static final Foo instance = new Foo();

for most singletons?  What is the actual, real likelihood of NOT using a 
singleton?   No, mostly (99.9% of the time[1]) you use it!  And what's 
the /real/ cost of schlepping around the "extra baggage" of an instance 
in the miniscule number of cases where you don't actually use it?  A 
couple of dozen bytes in memory?

Let's get over it.  Look where premature optimisation got us in '99 ;-)

Of course someone is bound to bleat about the very occasional
expensive/long-running init, but then I'd argue that you're likely to be 
much better of with a different solution that "static for singleton" -- 
maybe an asynchronous init with the possibility of failure in the 
meantime...  or something.  (Tradeoffs again.)

[BTW: I think this is probably the most interesting discussion thread 
we've seen on the list in a year!]

[1] Like 83.7% of all statistics, all numbers used herein are made up on 
the spot.
  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
-~----------~----~----~----~------~----~------~--~---

Reply via email to