JAVA单例模式--饿汉式单例模式

饿汉式单例类

class Danli {
	private static Danli ta = new Danli();
	public static Danli getInstance(){
		return ta;
	}
}

public class TestDanli{
	public static void main(String arg[]){
		Danli dl1 = Danli.getInstance();
		Danli dl2 = Danli.getInstance();
		if(dl1.equals(dl2))
			System.out.print("dl1==dl2");
	} 
}

输出结果:dl1==dl2

 

你可能感兴趣的:(java)