我是一名有用的助手,可以为您翻译文本。
我有一个关于枚举的问题。
我有一个枚举类,如下所示:
public enum FontStyle {
NORMAL("This font has normal style."),
BOLD("This font has bold style."),
ITALIC("This font has italic style."),
UNDERLINE("This font has underline style.");
private String description;
FontStyle(String description) {
this.description = description;
}
public String getDescription() {
return this.description;
}
}
我想知道这个枚举对象是何时创建的。
枚举看起来像是一个 'static final' 对象,因为其值永远不会改变。因此,在编译时仅初始化是高效的。
但是它在顶部调用了自己的构造函数,所以我怀疑它是否会在我们调用它时生成,例如在 switch 语句中。