Stats

Tuesday 16 June 2015

How to implement singleton class in java?

Hello All,

Hope you will enjoy this article.

How to implement singleton class in java?

Concept of Singleton : Singleton class is one for which only one instance i.e. object of it will be created.

To achieve this requirement it is important create first time the object for the class but afterwards whenever there is need for instance of that class .. reference for already created instance should be returned.

Following code will serve as an example for singleton class :

public class Singleton {

private static Singleton;
protected Singleton()                   //protected constructor
{
}

public static getInstance(){
{
           if(Singleton == null)
                      Singleton = new Singleton();
           return Singleton;
}
}

No comments:

Post a Comment