An example of declaring an enum inside a class : enum « Object Oriented « SCJP






class MyClass2 {
  enum Problem {BIG, HUGE, SMALL }
  
  Problem size;
}

public class MainClass {
  public static void main(String[] args) {
    MyClass2 drink = new MyClass2();
    drink.size = MyClass2.Problem.BIG;   // enclosing class name required
  }
}








6.11.enum
6.11.1.Java enum
6.11.2.Declaring Enums
6.11.3.An example of declaring an enum inside a class
6.11.4.You cannot declare enums in a method
6.11.5.It is optional to put a semicolon at the end of the enum declaration
6.11.6.Declaring Constructors, Methods, and Variables in an enum
6.11.7.Enums are subclasses of java.lang.Enum.
6.11.8.You can declare an enum anywhere you can declare a class.
6.11.9.Enums inherit data and methods from Object.
6.11.10.Enums may be converted and cast according to the same rules that govern any class that extends Object.
6.11.11.Enums may have main() methods and can be invoked as applications.
6.11.12.Enums that have no explicit constructors get default no-args constructors.
6.11.13.Enums have built-in name() and toString() methods, both of which return the name of the current instance.
6.11.14.You can add data, methods, and constructors to an enum.