Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Singleton – enum implementation

farenda 2016-08-23 0

Java Singleton – enum implementation

Singleton as enum is very simple to implement and very safe at the same time thanks to language guarantees for enum instantiation and serialization.

Properties of enum Singleton

  • works in all versions since Java 5
  • easy to implement
  • is serializable out of the box

Complete example

In this example we’ll create enum Singleton with dummy constructor – just to show when it is initialized. Java language specification guarantees that it will be initialized only once, as we want:

package com.farenda.patterns.singleton;

public enum SingletonEnum {

    INSTANCE;

    SingletonEnum() {
        System.out.println("Singleton enum initialization!");
    }

    int sum(int x, int y) {
        System.out.printf("Called with x = %d, y = %d%n", x, y);
        return x+y;
    }

    public static void main(String[] args) {
        System.out.println("Is singleton: "
            + (SingletonEnum.INSTANCE == INSTANCE));
        System.out.println("Sum: " + INSTANCE.sum(3, 4));
    }
}

The above code produces the following output:

Singleton enum initialization!
Is singleton: true
Called with x = 3, y = 4
Sum: 7

References:

  • Effective Java (2nd Edition) by Josh Bloch
  • GOF Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
  • Java Singleton – static final implementation
Share with the World!
Categories Patterns Tags design-patterns, gof, java
Previous: Java Singleton – on demand initialization
Next: Prime numbers – Sieve of Eratosthenes in Java

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored