单例设计模式
class Singleton{ private static final Singleton INSTANCE = new Singleton(); //在类内部封装对象 public static Singleton getInstance(){ return INSTANCE; } private Singleton(){ //构造方法私有化 } public void print(){ System.out.println("HelloWorld!"); } } public class helloworld { public static void main(String[] args) { Singleton inst = null; inst = Singleton.getInstance(); inst.print(); } }