- package demo;
- public class Singleton {
- private static Singleton s;
- public static Singleton getInstance(){
- if(s==null){
- s = new Singleton();
- }
- return s;
- }
- private Singleton(){
- // construct method
- }
- public static void main(String[] args) {
- Singleton s = Singleton.getInstance();
- Singleton s2 = Singleton.getInstance();
- System.out.println(s==s2);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1809201410480.html
来源: http://www.codesnippet.cn/detail/1809201410480.html