• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton concept in Java  RSS feed

 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just going through the following site to know more about singletons :
http://java.sun.com/developer/technicalArticles/Programming/singletons/

but i feel that its way to advanced to understand. I know basic ways of creating singletons and their uses.
I would like to know more about Singletons. Is there a good page where it explains Singletons in deep.
I have heard Enums being Singletons; would like to know about that too.


I encountered the following piece of code used for creating a Singleton:


Can anyone explain why are using a static class over here to instantiate the Singleton class ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea, it's useless.

The singleton pattern is terrible for many situations. If you find yourself writing something like it, slap yourself first and then think again if you really need it.

Yes, enums can be used as singletons. If you declare an enum type with only one constant, you effectively have a singleton. This already shows the bigger picture: singletons are a very specific case of instance controlled classes. For constants, enums are almost always preferable. For anything but constants, you might as well just make a regular class without worrying about how many instances of it you can create.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually a valid thread-safe lazy initialization idiom that makes use of how - or rather when - a class is loaded according to the JLS specification.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:It's actually a valid thread-safe lazy initialization idiom that makes use of how - or rather when - a class is loaded according to the JLS specification.

True; however:

@adithya: You almost never need it. I've used it precisely once in 12 years of Java programming. Lazy initialization is almost always the wrong way to go. Just use:
private static final Singleton INSTANCE = new Singleton();

And if you ever do use it; document it.

Winston
 
adithya narayan
Ranch Hand
Posts: 79
Android Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, why would one require
thread-safe lazy initialization idiom
?
I was just going through the following link :
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2

Form what little i understood, the JVM is intelligent enough to avoid dead-locks when
multiple threads simultaneously try instantiating objecs of a class. Correct me if i am wrong !
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:Well, why would one require
thread-safe lazy initialization idiom
?
I was just going through the following link :
http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2

Form what little i understood, the JVM is intelligent enough to avoid dead-locks when
multiple threads simultaneously try instantiating objecs of a class. Correct me if i am wrong !


It's usually applied as an alternative to double-checked locking when eager initialization is deemed inappropriate.
This approach works, because the SingletonHolder class is loaded and initialized "just in time" when the Singleton's getInstance() method is first called, and the static Singleton instance of SingletonHolder gets initialized as part of this process. Indeed, according to the JLS, a JVM must ensure that initialization of a class is properly synchronized. This implies that, however many threads invoke Singleton's getInstance() method however many times, the static Singleton instance of the SingletonHolder class is initialized exactly once.

And just to be clear, eventhough in my opinion this particular lazy initialization idom is far clearer, easier to understand and less prone to error than the double-checked locking approach, I'd seriously consider just using eager initialization.
Also, I tend to avoid the Singleton pattern altogether, so I'm definitely with Stephen and Winston on this.


 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adithya narayan wrote:Form what little i understood, the JVM is intelligent enough to avoid dead-locks when
multiple threads simultaneously try instantiating objecs of a class. Correct me if i am wrong !

Not wrong, which is precisely why the idiom works. What it was "invented" to avoid was:which is otherwise the simplest form of lazy instantiation (no double-check idioms or any of that nonsense).

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!