aspose file tools*
The moose likes Java in General and the fly likes Singleton - Initialization On Demand Holder. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Singleton - Initialization On Demand Holder." Watch "Singleton - Initialization On Demand Holder." New topic
Author

Singleton - Initialization On Demand Holder.

Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Hi,
I have question regarding the approach used for lazy loading of singletons.

As per IODH, we have a static inner class which takes care of assigning the singleton instance to the static variable.

The reason given is that JLS guarantees that JVM will not initialize it until it is referenced.

What I find confusing is that if I plainly use this inside a class..
static Singleton instance = new Singleton();

this will also get initialized only when the class i referred..

How different is this from IODH.


Somebody please enlighten me..

Thanks
Yogi


Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Yogesh Gnanapraksam wrote:Somebody please enlighten me..

The normal (non-lazy) pattern for a singleton is:all IODH does is to put the instance in a Holder class, viz:the assurance you speak of is that the Holder class will not be instantiated until it's required; and since that doesn't occur until you call instanceOf(), it works just fine.

That said, these things are often called anti-patterns for a reason. Their use is extremely rare; and if you can find another way of doing it you probably should. The only reasons that I can think of for using it are:
1. If the object itself takes a prohibitively long time to instantiate.
2. The object takes up an awful lot of space. I have used it myself for this reason to allow for use in resource-limited situations such as MIDP and CLDC (of course, you then have to provide an alternative).

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
Thanks Winston.
The problem I have is in understanding when the instantiation will occur.

In the non-lazy loading example ..
When will the Singleton class get loaded, will it load before I refer to the static variable 'theOne' ... ?


Thanks
Yogi
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

how do you wake-up in morning? using alarm? If yes then you loaded/wake-up by lazy initialization!
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
I still don't understand, I will try to find more about class loading.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Yogesh Gnanapraksam wrote:...In the non-lazy loading example ..
When will the Singleton class get loaded, will it load before I refer to the static variable 'theOne' ... ?

Yes, it'll happen as soon as
private static final Singleton theOne = new Singleton();
is encountered, and because it's a static variable, that'll be at Class load time.

I still don't understand, I will try to find more about class loading.

I have a feeling you're overthinking this, but don't let me stop you from researching. All good information is good; and the JLS is first-rate.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Singleton - Initialization On Demand Holder.