Singleton Pattern Using Java

 package demo;


public class SingletonPattern {


private static SingletonPattern obj = null;


private SingletonPattern() {


}


public static SingletonPattern getInstance() {

if (obj == null) {

obj = new SingletonPattern();

}

return obj;

}


public static void main(String[] args) {

SingletonPattern obj = getInstance();

SingletonPattern obj1 = getInstance();

System.out.println(obj==obj1); //true no new obj is created

}

}


Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)