Enum Classes (enum) are like a normal Classes, but restricted:
- the only instances are the declared constants; no new instance can be created at run time, not even using reflection or
clone;
- direct superclass is
Enum<E>;
- cannot be extended;
- cannot be declared
abstract
- some restrictions involving modifiers.
Despite of that (and some points I forgot), everything that can be done with a normal class, can be done with an enum: use all kind of fields and methods. Even each Enum Constant can have its own fields or methods.
BUT, as already commented, not everything is meaningful or recommended - like having a state, non-final fields.
public class Scene {
public enum Type { // maybe SceneType?
MENU,
PLAY;
private void switchTo() { // instance method, just an example
current = this; // bad example, should not change `Scene`'s state
}
}
public static Type current = Type.MENU;
public static void switchTo(Type type) {
Objects.requireNonNull(type);
current = type;
/* alternative, to show switch usage
switch (type) {
case PLAY:
current = Type.PLAY;
break;
case MENU:
...
*/
}
// just for demonstration, not the best solution
public static void switchToMenu() {
Type.MENU.switchTo();
}
public static void switchToPlay() {
Type.PLAY.switchTo();
}
}
not tested, no IDE was harmed (used), minor errors left for the user to solve
I do recommend, for above simple case, to only use the switchTo(Type) method in Scene; not the switchTo() in Type - added just as an example of what can be done. The Type.switchTo() method is strange, it is accessing a field of Scene, that is being more than just a type, wrong place for such functionality.
Obs: since Enum Classes work like normal classes, they can also be declared in an own file. But for the posted code, since bound to Scene, it can stay as a nested enum - must be accessed like Scene.Type.PLAY from other classes. (Your SCENE looks like being nested itself - static keyword not allowed on top level class - so the real path would be even longer, likeSomeClass.Scene.Type.PLAY)
Links: