HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Enum / Is enum Really Best Singletons?

Is enum Really Best Singletons?

You must have heard multiple times that enum are always the best choice for implementing singleton design pattern in java. Are they really best? If it is then how it is better than other available techniques? Let’s find out.

Writing a singleton implementation is always tricky. I discussed already couple of methods (including my favorite method as well) in my this blog post. I have written there clearly that enums provide implicit support for thread safety and only one instance is guaranteed. This is also a good way to have singleton with minimum effort.

Problems with enum as singleton

Having said that, like with any other thing in the universe, this approach does have it’s disadvantages which you will need to consider before making any decision.

  1. By default, enums do not support lazy loading.
  2. Though it’s very very rare but if you changed your mind and now want to convert your singleton to multi-ton, enum would not allow this.

If above both cases are no problem for anybody, the enum is probably the best choice.

Anyways, on side note, after compilation java enums are converted to classes only with additional methods e.g. values() and valueOf()… etc.

enum based singleton example

Once you have decided to write enum based singleton, writing it is really easy e.g.

enum Singleton
{
	INSTANCE;
	// instance vars, constructor

	private final Connection connection;
	
	Singleton()
	{
		// Initialize the connection
		connection = DB.getConnection();
	}

	// Static getter
	public static Singleton getInstance()
	{
		return INSTANCE;
	}

	public Connection getConnection()
	{
		return connection;
	}
}

Now you use can use final Singleton s = Singleton.getInstance(). Remember that since this is an enum you can always access this via Singleton.INSTANCE as well.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Jimis

    February 28, 2020

    How do you reach this conclusion: “enums do not support lazy loading.” What about people saying enum is laizy loaded as found in https://stackoverflow.com/questions/15470433/single-element-enum-type-singletone-with-lazy-loading-capability/15470565#15470565 , https://stackoverflow.com/questions/5842273/java-lazy-initializing-singleton/5843514#5843514 and https://stackoverflow.com/questions/16771373/singleton-via-enum-way-is-lazy-initialized/16771449#16771449

    • Lokesh Gupta

      February 29, 2020

      Thanks for the feedback. Probably, I should say “by default”. Edited the post.

  2. Ravinder

    December 16, 2019

    connection = DB.getConnection(); unable to understand what is DB here .this is a class or an interface

  3. not me

    October 21, 2017

    3. you can’t create subclasses of an enum-based singleton

Comments are closed on this article!

Search Tutorials

Java Enum Tutorial

  • Java Enum – Complete Guide
  • Java Enum – String Constants
  • Java Enum – Enum Singleton
  • Enumerator vs Iterator

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces