Java Singletons using enum type (The best method for making Singletons in Java)

Traditional methods of making singletons

Method 1: Singleton with public static final field

Method 2: Singleton with public static factory method

Method 3: Singleton with lazy initialization

Pros and Cons of above methods

public static Singleton getInstance() {    return new Singleton ();}
  1. Serialization and deserialization
  2. Reflection

Problems with serialization and deserialization

Two objects are not same
2
1
Two objects are same
2
2

Problems with Reflection

Two objects are not same
1
2

Making Singletons with enum in Java

Singleton with enum

How to use

--

--

--

Senior Software Engineer | Full Stack Developer | DevOps Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Testnet Update: New Borrower Pools & Interest Rate Update

Spring Core: IoC and Container

Dictionary in Python

Weekly update #1

Practical use of key paths in MVVM

The Evolution of My Productivity as a Freelancer

Fundamentals of Clean Code

Introduction to Istio Traffic Management

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dulaj Atapattu

Dulaj Atapattu

Senior Software Engineer | Full Stack Developer | DevOps Engineer

More from Medium

Java Packages & API-10 example

Java Newsletter — Issue 12

Java Concurrency API

Spring vs Spring Boot