What is the use of Sigleton Classes

Discussions

General J2EE: What is the use of Sigleton Classes

  1. What is the use of Sigleton Classes (1 messages)

    Hi..Java Programmers..!

    Can You please tell the use of Singletons in Programming,
    And how this would be useful in Applications.

    Thanks in Adavance,
    Sivanand
  2. using singleton pattern, only a single instance of a class exists which can be used by others. It does not allow creation of new objects of the same class on the heap.

    An exaple
    public class Singleton{
     
      private Singleton(){
      }

      private static Singleton cInstance;
      public static Singleton getInstance(){
        /** only create an instance if there is none*/
        if(cInstance == null){
           cInstance = new cInstance();
        }
        return cInstance;
      }

      public void yourMethod(){
      }
    }