You can have a nested enum type declaration.
You can declare a nested enum type inside a class, an interface, or another enum type.
Nested enum types are implicitly static.
You can declare a nested enum type static explicitly in its declaration.
Since an enum type is always static, you cannot declare a local enum type (e.g. inside a method's body).
You can use any of the access modifiers (public, private, protected, or package) level for a nested enum type.
The following code declares a nested public enum type named Gender inside a Person class.
class Person { public enum Gender { MALE, FEMALE//from w w w.j a v a2s . co m } } public class Main { public static void main(String[] args) { Person.Gender m = Person.Gender.MALE; Person.Gender f = Person.Gender.FEMALE; System.out.println(m); System.out.println(f); } }
You can use the simple name of an enum constant by importing the enum constants using static imports.
import com.book2s.Person.Gender; import static com.book2s.Person.Gender.*; class Test { public static void main(String[] args) { Gender m = MALE; Gender f = FEMALE; System.out.println(m); System.out.println(f); } }
You can nest an enum type inside another enum type or an interface.
The following are valid enum type declarations:
enum OuterEnum { C1, C2, C3;// ww w . java2s .c o m public enum NestedEnum { C4, C5, C6; } } interface MyInterface { int operation1(); int operation2(); public enum AnotherNestedEnum { CC1, CC2, CC3; } }