Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singleton - Initialization On Demand Holder.

 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do you wake-up in morning? using alarm? If yes then you loaded/wake-up by lazy initialization!
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand, I will try to find more about class loading.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic