In software engineering, the singleton design pattern is used to restrict instantiation of a class to one
object. This is useful when exactly one object is needed to coordinate actions across the system.
Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist.
It is also considered an anti-pattern since it is often used as a euphemism for global variable. Before
designing a class as a singleton, it is wise to consider whether it would be enough to design a normal class
and just use one instance.

The singleton pattern is implemented by creating a class with a method that creates a new instance of the
object if one does not exist. If an instance already exists, it simply returns a reference to that object. To
make sure that the object cannot be instantiated any other way, the constructor is made either private or
protected. Note the distinction between a simple static instance of a class and a singleton. Although a
singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or
resources until needed.

The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to
execute the creation method at the same time when a singleton does not yet exist, they both must check for
an instance of the singleton and then only one should create the new one. If the programming language has
concurrent processing capabilities the method should be constructed to execute as a mutually exclusive
operation.

The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is
being instantiated.

Here is the Example of Singleton Class with Unique DataBase Object.

package com;

import java.sql.*;

   public class Connect
   {
       private Connection conn = null;
       private static Connect obj=null;
       
       private Connect()
       {
       }
       public Connection getConnection()
       {
           try
           {
               String userName = "root";
               String password = "";
               String url = "jdbc:mysql://localhost:3306/geekmantra";

               Class.forName("com.mysql.jdbc.Driver");

               conn = DriverManager.getConnection(url, userName, password);

               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.err.println ("Cannot connect to database server-"+e);
           }
          return conn;
       }

       public void closeConnection()
       {
           if (conn != null)
           {
               try
               {
                   conn.close ();
                   System.out.println ("Database connection terminated");
               }
               catch (Exception e) { /* ignore close errors */ }
           }
        }
  
            public static Connect getData()
         {
            if(obj==null)
            {
                obj=new Connect();
            }
            return obj;               
         }
 
   }