If you’re working with Spring, check out "REST With Spring":

>> CHECK OUT THE COURSE

1. Introduction

In this quick article, we’ll discuss the two most popular ways of implementing Singletons in plain Java.

2. Class-Based Singleton

The most popular approach is to implement a Singleton by creating a regular class and making sure it has:

  • A private constructor
  • A static field containing its only instance
  • A static factory method for obtaining the instance

We’ll also add an info property, for later usage only. So, our implementation will look like this:

public final class ClassSingleton {

    private static ClassSingleton INSTANCE;
    private String info = "Initial info class";
    
    private ClassSingleton() {        
    }
    
    public static ClassSingleton getInstance() {
        if(INSTANCE == null) {
            INSTANCE = new ClassSingleton();
        }
        
        return INSTANCE;
    }

    // getters and setters
}

While this is a common approach, it’s important to note that it can be problematic in a multithreading scenarios, which is the main reason for using Singletons.

Simply put, it can result in more than one instance, breaking the pattern’s core principle. Although there are definitely locking solutions for this problem, our next approach solves these problems at a root level.

3. Enum Singleton

Moving forward, let’s not discuss another interesting approach – which is to use enumerations:

public enum EnumSingleton {
    
    INSTANCE("Initial class info"); 
 
    private String info;
 
    private EnumSingleton(String info) {
        this.info = info;
    }
 
    public EnumSingleton getInstance() {
        return INSTANCE;
    }
    
    // getters and setters
}

This approach has serialization and thread-safety guaranteed by the enum implementation itself, which ensures internally that only the single instance is available, correcting the problems pointed out on the class-based implementation.

4. Usage

In order to use our ClassSingleton, we simply need to get the instance statically:

ClassSingleton classSingleton1 = ClassSingleton.getInstance();

System.out.println(classSingleton1.getInfo()); //Initial class info

ClassSingleton classSingleton2 = ClassSingleton.getInstance();
classSingleton2.setInfo("New class info");

System.out.println(classSingleton1.getInfo()); //New class info
System.out.println(classSingleton2.getInfo()); //New class info

As for the EnumSingleton, we can use it as any other Java Enum:

EnumSingleton enumSingleton1 = EnumSingleton.INSTANCE.getInstance();

System.out.println(enumSingleton1.getInfo()); //Initial enum info

EnumSingleton enumSingleton2 = EnumSingleton.INSTANCE.getInstance();
enumSingleton2.setInfo("New enum info");

System.out.println(enumSingleton1.getInfo()); // New enum info
System.out.println(enumSingleton2.getInfo()); // New enum info

5. Conclusion

In this quick tutorial, we focused on how to implement the Singleton pattern using only core Java, and how to make sure it’s consistent and how to make use of these implementations.

The full implementation of these examples can be found over on GitHub.

The new Certification Class of "REST With Spring" is finally out:

>> CHECK OUT THE COURSE

Leave a Reply

1 Comment on "Singletons in Java"

Notify of
avatar
Sort by:   newest | oldest | most voted
Alejandro Gervasio
Guest

Hi Cleverson,

Very nice tutorial! short and straight to the point. It’s really good to see you added the thread-safe implementation of the pattern.

wpDiscuz