慕森王
构造方法私有化的话,这个类就无法在其他地方创建对象。可以参考单例模式。例如下面的代码。属于单例模式的懒汉式。单例模式的好处是1、提供了对唯一实例的受控访问。2、由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象单例模式无疑可以提高系统的性能。3、允许可变数目的实例。1234567891011public class Singleton { private static Singleton instance; private Singleton (){} public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }