This,

public enum MySingleton {
  INSTANCE;   
}

has an implicit empty constructor. Make it explicit instead,

public enum MySingleton {
    INSTANCE;
    private MySingleton() {
        System.out.println("Here");
    }
}

If you then added another class with a main() method like

public static void main(String[] args) {
    System.out.println(MySingleton.INSTANCE);
}

You would see

Here
INSTANCE

enum fields are compile time constants, but they are instances of their enum type. And, they're constructed when the enum type is referenced for the first time.

Answer from Elliott Frisch on Stack Overflow
๐ŸŒ
DZone
dzone.com โ€บ coding โ€บ java โ€บ java singletons using enum
Java Singletons Using Enum
July 21, 2017 - Since enums are inherently serializable, we don't need to implement it with a serializable interface. The reflection problem is also not there. Therefore, it is 100% guaranteed that only one instance of the singleton is present within a JVM. Thus, this method is recommended as the best method of making singletons in Java.
Discussions

What are the downsides of implementing a singleton with Java's enum? - Software Engineering Stack Exchange
I agree that this is a rare and contrived corner case, and almost always an enum can be used for a java singleton. I do it myself. More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
Implementing Singleton as enum?
No other structure guarantees reflection resistance. The only downfall is that you won't get lazy initialization. More on reddit.com
๐ŸŒ r/java
83
45
March 21, 2017
java - Singleton using enum - Code Review Stack Exchange
This is enough; you can directly use Singleton.INSTANCE to get the instance of the class. ... public enum Singleton { INSTANCE; public void execute (String arg) { //... perform operation here ... More on codereview.stackexchange.com
๐ŸŒ codereview.stackexchange.com
August 6, 2016
java - How to create a singleton class using enum - Stack Overflow
I am trying to create a singleton class in Java. The best available solution with Java5 and above versions seems to be using enum. But I am not sure how to convert my class into a singleton class u... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-make-a-singleton-enum-in-java
How to make a singleton enum in Java?
A singleton enum in Java is a type-safe and concise way to implement the singleton design pattern, which ensures that a class has only one ( or single) instance throughout the application.
Top answer
1 of 3
32

Some problems with enum singletons:

Committing to an implementation strategy

Typically, "singleton" refers to an implementation strategy, not an API specification. It is very rare for Foo1.getInstance() to publicly declare that it'll always return the same instance. If needed, the implementation of Foo1.getInstance() can evolve, for example, to return one instance per thread.

With Foo2.INSTANCE we publicly declare that this instance is the instance, and there's no chance to change that. The implementation strategy of having a single instance is exposed and committed to.

This problem is not crippling. For example, Foo2.INSTANCE.doo() can rely on a thread local helper object, to effectively have a per-thread instance.

Extending Enum class

Foo2 extends a super class Enum<Foo2>. We usually want to avoid super classes; especially in this case, the super class forced on Foo2 has nothing to do with what Foo2 is supposed to be. That is a pollution to the type hierarchy of our application. If we really want a super class, usually it's an application class, but we can't, Foo2's super class is fixed.

Foo2 inherits some funny instance methods like name(), cardinal(), compareTo(Foo2), which are just confusing to Foo2's users. Foo2 can't have its own name() method even if that method is desirable in Foo2's interface.

Foo2 also contains some funny static methods

    public static Foo2[] values() { ... }
    public static Foo2 valueOf(String name) { ... }
    public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name)

which appears to be nonsensical to users. A singleton usually shouldn't have pulbic static methods anyway (other than the getInstance())

Serializability

It is very common for singletons to be stateful. These singletons generally should not be be serializable. I can't think of any realistic example where it makes sense to transport a stateful singleton from one VM to another VM; a singleton means "unique within a VM", not "unique in the universe".

If serialization really does make sense for a stateful singleton, the singleton should explicitly and precisely specify what does it means to deserialize a singleton in another VM where a singleton of the same type may already exist.

Foo2 automatically commits to a simplistic serialization/deserialization strategy. That is just an accident waiting to happen. If we have a tree of data conceptually referencing a state variable of Foo2 in VM1 at t1, through serialization/deserialization the value becomes a different value - the value of the same variable of Foo2 in VM2 at t2, creating a hard to detect bug. This bug won't happen to the unserializable Foo1 silently.

Restrictions of coding

There are things that can be done in normal classes, but forbidden in enum classes. For example, accessing a static field in the constructor. The programmer has to be more careful since he's working in a special class.

Conclusion

By piggybacking on enum, we save 2 lines of code; but the price is too high, we have to carry all the baggages and restrictions of enums, we inadvertently inherit "features" of enum that have unintended consequences. The only alleged advantage - automatic serializability - turns out to be a disadvantage.

2 of 3
7

An enum instance is dependant on the class loader. ie if you have a second class loader that does not have the first class loader as a parent loading the same enum class you can get multiple instances in memory.


Code Sample

Create the following enum, and put its .class file into a jar by itself. ( of course the jar will have the correct package/folder structure )

package mad;
public enum Side {
  RIGHT, LEFT;
}

Now run this test, making sure that there is no copies of the above enum on the class path:

@Test
public void testEnums() throws Exception
{
    final ClassLoader root = MadTest.class.getClassLoader();

    final File jar = new File("path to jar"); // Edit path
    assertTrue(jar.exists());
    assertTrue(jar.isFile());

    final URL[] urls = new URL[] { jar.toURI().toURL() };
    final ClassLoader cl1 = new URLClassLoader(urls, root);
    final ClassLoader cl2 = new URLClassLoader(urls, root);

    final Class<?> sideClass1 = cl1.loadClass("mad.Side");
    final Class<?> sideClass2 = cl2.loadClass("mad.Side");

    assertNotSame(sideClass1, sideClass2);

    assertTrue(sideClass1.isEnum());
    assertTrue(sideClass2.isEnum());
    final Field f1 = sideClass1.getField("RIGHT");
    final Field f2 = sideClass2.getField("RIGHT");
    assertTrue(f1.isEnumConstant());
    assertTrue(f2.isEnumConstant());

    final Object right1 = f1.get(null);
    final Object right2 = f2.get(null);
    assertNotSame(right1, right2);
}

And we now have two objects representing the "same" enum value.

I agree that this is a rare and contrived corner case, and almost always an enum can be used for a java singleton. I do it myself. But the question asked about potential downsides and this note of caution is worth knowing about.

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ singletons in java
Singletons in Java | Baeldung
October 23, 2025 - While this is a common approach, ... using Singletons. Simply put, it can result in more than one instance, breaking the patternโ€™s core principle. Although there are locking solutions to this problem, our next approach solves these problems at a root level. Moving forward, letโ€™s discuss another interesting approach, which is to use enumerations...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java enum โ€บ are java enums really the best singletons?
Are Java Enums Really the Best Singletons?
April 4, 2023 - I already discussed a couple of methods (including my favorite method as well) in 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 a singleton with minimum effort.
Find elsewhere
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-use-the-singleton-pattern-using-enum-in-java
How to use the singleton pattern using enum in Java
enum Singleton { INSTANCE; private final Client dbClient; Singleton() { dbClient = Database.getClient(); } public static Singleton getInstance() { return INSTANCE; } public Client getClient() { return dbClient; } }
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 07 โ€บ why-enum-singleton-are-better-in-java.html
Why Enum Singleton are better in Java? Examples
Enum Singletons are new way to implement Singleton pattern in Java by using Enum with just one instance. Though Singleton pattern in Java exists from long time Enum Singletons are relatively new concept and in practice from Java 5 onwards after ...
๐ŸŒ
Medium
dulajra.medium.com โ€บ java-singletons-using-enum-type-the-best-method-for-making-singletons-in-java-6101048f4f31
Java Singletons using enum type (The best method for making Singletons in Java) | by Dulaj Atapattu | Medium
July 5, 2017 - Since enums are inherently serializable we donโ€™t need to implement it with serializable interface. Reflection problem is also not there. Therefore, it is 100% guaranteed that only one instance of the singleton is present within a JVM. Thus, this method is recommended as the best method of making singletons in java.
๐ŸŒ
Reddit
reddit.com โ€บ r/java โ€บ implementing singleton as enum?
r/java on Reddit: Implementing Singleton as enum?
March 21, 2017 -

In Effective Java 2nd Edition, Joshua Bloch recommends implementing singleton using ENUM with a single item:

As of release 1.5, there is a third approach to implementing singletons. Simply make an enum type with one element:

// Enum singleton - the preferred approach
public enum Elvis {
    INSTANCE;

    public void leaveTheBuilding() { ... }
}

This approach is functionally equivalent to the public field approach, except that it is more concise, provides the serialization machinery for free, and provides an ironclad guarantee against multiple instantiation, even in the face of sophisticated serialization or reflection attacks. While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

While this practice solves some of the pitfalls of singletons, I always felt it is misuse and abuse of Enum construct for something that it was not intended for? What do you thing about this approach?

๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 707258 โ€บ engineering โ€บ Singleton-Pattern-Enum-Singleton
Singleton Pattern - Doubt with Enum Singleton. (OO, Patterns, UML and Refactoring forum at Coderanch)
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 ...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-singleton-enum
Java Singleton Enum - Javatpoint
Java Singleton Enum with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
CodinGame
codingame.com โ€บ playgrounds โ€บ 6273 โ€บ design-pattern-singleton-using-enum
Design Pattern โ€“ Singleton using Enum
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ implement singleton pattern with a single-element enum
Java Language Tutorial => Implement Singleton pattern with a...
Therefore, that allows to implement ... point } } According to "Effective Java" book by Joshua Bloch, a single-element enum is the best way to implement a singleton....
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 07 โ€บ why-enum-singleton-are-better-in-java.html
Javarevisited: Why Enum Singleton are better in Java? Examples
Learn Java, Programming, Spring, Hibernate throw tutorials, examples, and interview questions ยท Enum Singletons are new way to implement Singleton pattern in Java by using Enum with just one instance.
๐ŸŒ
Medium
medium.com โ€บ @memansour96 โ€บ understanding-the-singleton-design-pattern-enums-challenges-and-why-its-often-criticized-as-an-f3b32ed3a851
Understanding the Singleton Design Pattern: Enums, Challenges, and Why Itโ€™s Often Criticized as an Anti-Pattern | by Mahmoud Mansour | Medium
December 28, 2024 - ... Now, the DatabaseManager is an enum with a single constant, INSTANCE. In Java, an enum with a single constant is a thread-safe and serialization-safe implementation of the Singleton design pattern.