• Post Reply Bookmark Topic Watch Topic
  • New Topic

The instance variable in "initialization on demand holder"  RSS feed

 
T . M Badr
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At Genesh and Sharma book, what's the purpose of the instance variable myInstance at the following code that implements the singleton pattern using "initialization on demand holder"

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what the context of that code is, i.e. what the book is saying about it, but considered as a standalone piece of code the myInstance variable is useless. It's never used anywhere in the code. Actually it's worse than useless because it's public, and the unwary user could assume it contained a reference to a Logger. This would result in a NullPointerException.
 
T . M Badr
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the book says before the code :

Listing 5-11 shows another implementation of the Logger class that is based on the “initialization on demand holder” idiom. This idiom uses inner classes and does not use any synchronization construct (recall the discussion of inner classes in Chapter 4). It exploits the fact that inner classes are not loaded until they are referenced.

And that is what is said after the code:

Hmm…at last you’ve found an efficient working solution. However, before we close this discussion of the singleton design pattern, two parting words of caution. First, use the singleton pattern wherever it is appropriate, but do not overuse it. Second, make sure that your singleton implementation ensures the creation of only one instance even if your code is multi-threaded.
 
T . M Badr
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The strange thing is that they always declare the class object as public when using the singleton pattern, example:

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I agree that is strange. But this must be an old book you're reading. Since Java 6 the simplest way to provide a singleton is by using an enum:

 
T . M Badr
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book is to cover the exams 1Z0-804 and 1Z0-805, which are SE 7 exams, book name is:
Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805
Authors: S G Ganesh, Tushar Sharma
Published: February 27, 2013
ISBN-10: 1430247649
ISBN-13: 978-1430247647
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, it's one of those exam-training books. you should never ever consider code in those books to be examples of good code. Usually it's bad code in some way, just with the purpose of helping you to learn some feature of Java.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no.

The initialise on demand is a pointless exercise in every singleton I have ever seen.

Unless the class has another externally accessible static thing other than getInstance that is going to be called independently and prior to the call to getInstance then it achieves nothing.

So either do it without the holder, or do it the way Paul suggests using an enum.
Or don't do it at all as a singleton is not something people should generally be writing anymore. They're a swine to test if they have any dependencies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!