Singleton

WriterToFile.java

package tests.Singleton;

public class WriterToFile {

    private static WriterToFile instance;

    private WriterToFile() {
        instance = null;
    }

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

    public void write() {
        System.out.println("I am Writer! " + instance.toString());
    }
}

main.java

package tests.Singleton;

public class mail {

    public static void main(String[] args) {
        WriterToFile wtf = WriterToFile.getInstance();
        WriterToFile wtf1 = WriterToFile.getInstance();
        System.out.println("Hi");

        wtf.write();
        wtf1.write();
        
        if (wtf.equals(wtf1)) {
            System.out.println("EQUAL!");
        } else {
            System.out.println("not equal");
        }
    }
}