Understanding Java Singleton with Example

Singleton


Singleton is a design pattern purpose is to control object creation. Which means limiting the number of objects to only one.


Example


  1. public class Singleton {

  2.    private static Singleton singleton = null;
  3.    
  4.    //Private Constructor
  5.    private Singleton() {

  6.    }

  7.    public static Singleton getInstance() {
  8.       if(singleton == null) {
  9.          singleton = new Singleton();
  10.       }
  11.       return singleton;
  12.    }
  13. }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation