/**
* {@link <a href=
* "https://[Log in to view URL]" target=
* "_blank">Singleton factory</a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
public static class MySingleton {
private static MySingleton instance = null;
public int x = 10;
private MySingleton() {
;
}
public static MySingleton getInstance() {
if (instance == null)
instance = new MySingleton();
return instance;
}
}
public static void main(String args[]) throws Exception {
// Unit test - singleton factory
MySingleton a = MySingleton.getInstance();
MySingleton b = MySingleton.getInstance();
a.x = b.x + 10;
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: