能使用的单例代码

public class Singleton {
	
	private Singleton(){};
	
	private static class SingletonHolder{
		private static Singleton singleton=new Singleton();
	}
	public static Singleton getInstance(){
		
		return SingletonHolder.singleton;
	}
	
	public static void main(String[] args) {
		Singleton2.Singleton.sys();
		
	}
	
}


enum Singleton2{
	Singleton("test");
	
	private String param;
	private Singleton2(String param){
		this.param=param;
	}
	public void sys(){
		System.out.println(param);
	}
	
	
}

你可能感兴趣的:(java)