I wrote a program to call a singleton class from inside the class main. I also wrote an other class from which I am trying to create a object for the Singleton class. I found that I am able to create only one object when I tried from the other class but when I tried to call the method from the same class it creates more than one object. I couldnt understand what is different from same class main method and other class main method. Here are the classes:
SingleTonClass.java
public class SingleTonClass {
    private static SingleTonClass obj = null;
    private SingleTonClass() {
        System.out.println("Private Constructor is called");
    }
    public static SingleTonClass CreateObj() {
        if (obj == null) {
            obj = new SingleTonClass();
        }
        return obj;
    }
    public void display() {
        System.out.println("The Ojecte creation complete");
    }
    public void display1() {
        System.out.println("The second obj creation is comeplete");
    }
    public static void main(String[] args) {
        SingleTonClass stc = new SingleTonClass();
        SingleTonClass stc1 = new SingleTonClass();
        SingleTonClass stc2 = new SingleTonClass();
        // stc.display();
        // stc1.display1();
    }
}
SingleTonClassTest.java
public class SingleTonClassTest {
    public static void main(String[] args) {
        SingleTonClass stc=SingleTonClass.CreateObj();
        SingleTonClass stc1=SingleTonClass.CreateObj();
        SingleTonClass stc2=SingleTonClass.CreateObj();
    }
}
 
     
     
     
     
     
     
    