Check this singleton design pattern explained with programs and steps

public class Singleton {

static Singleton obj;
private  Singleton(){
}

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

public static void main(String[] args) {

Singleton obj=Singleton.getInstance();
Singleton obj1=Singleton.getInstance();

if(obj==obj1){
System.out.println("indhu");
}
else{
System.out.println("Sindhu");
}
               System.out.println(obj==obj1);

}
}


Output:
indhu
true

Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu