SCJP – Java Enums

  • enum CoffeeSize { BIG, HUGE, OVERWHELMING };
    • CoffeeSize cs = CoffeeSize.BIG;
  • enum CoffeeSize { BIG, HUGE, OVERWHELMING }; // <--semicolon  is optional here


  • CoffeeSize.values()  // returns a Collection of CoffeeSize enums. A Collection, not an array


  • Syntax example of a enum usage
public class WeatherTest {
   static Weather w;
   public static void main(String[] args) {
       System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
enum Weather {
   RAINY, Sunny;
   int count = 0;
   Weather() {
      System.out.print("c ");
  • All of an enum’s values are initialized at the same time, and an enum’s variables are treated as if they were instance variables, not static variables


  • Enum declaration
    • Can declaring an enum outside a class
    • Can declaring an enum inside a class
    • Cannot declare enums in methods


  • enum really is a special kind of class
    • You can add constructors, instance variables, methods,
    • and something really strange known as a constant specific class body.
  • Constant specific class body: looks like an anonymous inner class for one enum value
    • OVERWHELMING(16) { body: methods override }


  • == or equals on enums values evaluates true in both cases.
  • The member variables declared inside an enum is by default static.


  • You can NEVER invoke an enum constructor directly
    • The enum constructor is invoked automatically. For example, BIG(8) invokes the CoffeeSize(int) constructor.
  • You can define more than one argument to the constructor, and
    • you can overload the enum constructors, just as you can overload a normal class constructor.


Leave a Reply