commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oliver Heger <oliver.he...@oliver-heger.de>
Subject Re: [lang] LazyInitializer
Date Sat, 24 Oct 2009 15:35:30 GMT
Stephen Colebourne schrieb:
> I believe that we should remove this class.
> 
> The class performs no useful purpose, as there is a better way to use a 
> separate class to create a value lazily:
> 
> // Lazy initialization holder class idiom for static fields
> private static class FieldHolder {
>      static final FieldType field = computeFieldValue();
> }
> static FieldType getField() { return FieldHolder.field; }
> 
> http://java.sun.com/developer/technicalArticles/Interviews/bloch_effective_08_qa.html

> 
> 
> Since LazyInitializer requires the user to create a new instance of a 
> new class anyway, the benefits of the 'advanced' lazy initializer code 
> disappear (in fact, its worse, due to the volatile variable requiring a 
> memory check every time, whereas the regular check above sets a simple 
> static final.
> 
> Any justifications for keeping this?
> 

The lazy initialization holder class idiom only applies for static 
fields of a class. The LazyInitializer class uses the double-check idiom 
and is intended for lazy initialization of instance fields. So there may 
be different use cases for it.

Maybe this fact can be better emphasized in the documentation and/or the 
class name?

Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message