Java Enum Static Final Instance Variables
2022-09-03 03:30:36
哎呀!
这段代码工作了一段时间,然后我决定添加一个默认颜色,它停止工作了。我收到以下错误:
1 error found:
File: Status.java [line: 20]
Error: Status.java:20: illegal reference to static field from initializer
在编译时使用以下代码。
import java.awt.Color;
enum Status
{
OFF ("Off"),
TRAINING ("Training", new Color(255, 191, 128)),
BEGINNER ("Beginner", new Color(128, 255, 138)),
INTERMEDIATE ("Intermediate", new Color(128, 212, 255)),
ADVANCED ("Advanced", new Color(255, 128, 128));
public final String name;
public final Color color;
public static final Color defaultColor = Color.WHITE;
Status(String name)
{
this(name, defaultColor);
}
Status(String name, Color color)
{
this.name = name;
this.color = color;
}
}
据我所知,这应该有效,但无论出于何种原因,Java决定抛出一个错误。有什么想法吗?