I’ve noticed that there is no Java Singleton sample in my blog. Here it is:
MySingleton.java
public class MySingleton { private final static MySingleton instance = new MySingleton(); private String test = null; private MySingleton() { super(); } public static MySingleton getInstance() { return instance; } public String getTest() { return test; } public void setTest(String test) { this.test = test; } } |
Execution:
MySingleton singleton = MySingleton.getInstance(); singleton.setTest("This is a test."); |