现在,我正在考虑将私有构造函数添加到只包含某些String
常量的类。使用私有构造函数来防止类的实例化?
public class MyStrings {
// I want to add this:
private MyString() {}
public static final String ONE = "something";
public static final String TWO = "another";
...
}
是否有任何性能或内存开销,如果我添加一个私有构造函数,这个类来防止有人实例呢?
你认为这是必要的吗?或者私人构造函数为此目的是浪费时间和代码混乱?
UPDATE
我要与私有构造函数和类的描述性的javadoc final类。我不能使用ENUM(我更喜欢),因为现在我停留在Java 1.4上。这将是我的修改:
/**
* Only for static access, do not instantiate this class.
*/
public final class MyStrings {
private MyString() {}
public static final String ONE = "something";
public static final String TWO = "another";
...
}
更新区块是一个答案,如果有* *没有人 – 030 2018-02-19 22:02:35