Skip to main content

Posts

Showing posts with the label Singleton safe from Reflection and Serialization

Design Patterns - Singleton inJava with example

This post is about Singleton Design pattern in java. All of us may know, there are different way to create singleton instance of class. I am going to write one of them. How we can make singleton object. But before going further, we will first learn what singleton is? What is Singleton? Singleton is design pattern which comes under creational pattern. Sometimes, it is important to have one instance of class or we can say, it provide single global entry point to the object. How we can create Singleton? There are many ways to create Singleton. Each one of them have their pros and cons. Today, we will discuss how to create Singleton with lazy Initialization with SingletonInstanceHolder class. package singleton ; public class Singleton { private static class SingletonInstanceHolder { private static int MODCOUNT = 0 ; private static Singleton INSTANCE = null ; private static final Object MUTEX = new Object (); static { if ( INSTANCE == n