public class Singleton { private Singleton (); private static Singleton Instance =new Singleton (); public static Singleton getInstance (){ return Instance ; } }
public class Test{ public static void main(String[] args){ Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); if(s1==s2){ Systom.out.println("s1和s2是同一实例") }else{ Systom.out.println("s1和s2不是同一实例") } } }
以上是饿汉模式