This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Singleton Pattern - Doubt with Enum Singleton.  RSS feed

 
Ranch Hand
Posts: 328
Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

It is said that Enum singletons are the best way way to create singletons.

How ever I have one doubt.

Writing Enum Singleton is very simple.

Eg.


and we access it in the following way,

SingletonInstance.INSTANCE.doStuff();


However,

If we write two or more Enum Constants like:



doStuff() is accessible by all 3 INSTANCEs?

there are 2 possibilities:

1) 3 different Instances are created
2) Still at any point of time, irrespective of number of constants, only single instance is created?

If the 2nd possibility is true, then its a true singleton.

but if 1st possibility is true, Its like we are creating just one instance of the ENUM and saying its a singleton.

There should be a way to restrict the no of constants in the ENUM, to one and only one and not more than one to make the ENUM a Singleton.

I request your opinion on the same.

Regards,
-Pankaj.

 
Sheriff
Posts: 13187
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are mixing concepts.

The enum singleton is only a singleton if you define one enum value. If you define more than one value, then you have as many instances as you have values. In your case there would be three instances of the enum class. By definition, you no longer have a singleton at that point.

Enums were not explicitly meant to allow the creation of singletons. Someone just noticed that the way enums work would make them a better and perhaps cleaner way to essentially define a Singleton.
 
Pankaj Shet
Ranch Hand
Posts: 328
Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot for your reply Junilu,

But if there are three instances getting created, if there are three constants, than that's not the singleton.  There must be the way to restrict enums to create a single instance irrespective of number of ENUM Constants.

Otherwise I can definately create the singleton object using class by creating just a single object of that class in the same way I create the single ENUM constant, without writing any additional code of restricting that class?

What's your say?



 
Junilu Lacar
Sheriff
Posts: 13187
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, enums were not introduced into the language so that we could create singletons. It just so happens that the way enums work, it makes it a convenient mechanism for creating a singleton.

Your assertion that there should be a way to limit the number of enum values is like saying that because you can turn a car into a bed, there should be a way to register a bed so you can drive it on the highway.

Enums are primarily meant to define a finite set of constant values so that there is stricter type checking. Just because a single enum value can be used to represent a singleton doesn't mean the language should provide a means to limit the number of values defined. That is solely a design decision that is in the purview of the designer/programmer. If you don't want more than one value in your enum type, then just don't define more than one. It's that simple.
 
Junilu Lacar
Sheriff
Posts: 13187
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find out more about the real motivation for introducing enums to Java by searching for "java typesafe enum pattern" -- As you'll see, the motivation is primarily about type safety and not about creating Singleton objects.
 
Pankaj Shet
Ranch Hand
Posts: 328
Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your response.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!