Singleton in java

  1. Java Singleton Class Example Using Private Constructor. We can make constructor as private. So that We can not create an object outside of the class. This property is useful to create singleton class injavaSingleton pattern helps us to keep only one instance of a class at any time.


class Demo1
{
private Demo1()
{
System.out.println(" from demo 1 ");
}
static  Demo1 a=new Demo1();
 
public static Demo1 getInstance()
{
return a;
}

}

public class SingletonExampl 
     {
public static void main(String[] args) 
        {
Demo1 b=Demo1.getInstance();
}

}

Post a Comment

Previous Post Next Post