How do you create a class of enums without a instance variable?

Viewed 66

I have a class with a list of enums, The enums are passed to a constructor and updated to the toString, but I am not allowed to have an instance variable on the class (part of the requirement). How can I make the enums output like the String without adding an instance?

public enum Other {

    GAME_BOY("Game Boy"), MACBOOK("Macbook Pro"), IPHONE("iPhone XS"), LAPTOP("Laptop");

    private final String product; //can't have instance variable

    private Other(String passed) {
        this.product = passed;
    }

    @Override
    public String toString() {
        return product;
    }
}
4 Answers

You can override toString() for each element:

public enum Other {
   GAME_BOY {
     @Override public String toString() { return "Game Boy"; }
   },
   MACBOOK { ... },
   ...
}

See this code run live at IdeOne.com.

Alternatively, you can put a switch statement in the toString method:

@Override
public String toString() {
    switch (this) {
        case GAME_BOY:
            return "Game boy";
        case MACBOOK:
            return "Macbook Pro";
        ...
    }
}

How about the following:-

public enum Other {

    GAME_BOY, MACBOOK, IPHONE, LAPTOP;

  @Override
  public String toString() {

    switch(this) {
      case GAME_BOY:
        return "Game Boy";

      case MACBOOK:
        return "Macbook Pro";

      case IPHONE:
        return "iPhone XS";

      case LAPTOP:
        return "Laptop";

      default:
        return null;
      }
   }

  public static void main(String[] args) {

    System.out.println("The value of the other is " + Other.GAME_BOY.toString());
  }
}

Enhanced switch

The accepted Answer by Williamson provides code that can be improved in Java 14 and later. The enhanced switch statement can prevent the problem of missing any of the enum objects in your list of switch cases. See: JEP 361: Switch Expressions.

The new syntax is also terse, easier to read.

Using the new approach looks like this:

package work.basil.example;

public enum Device
{
    GAME_BOY, MACBOOK, IPHONE, LAPTOP;

    @Override
    public String toString ( )
    {
        return switch ( this )
                {
                    case GAME_BOY -> "Game boy";
                    case MACBOOK -> "Macbook Pro";
                    case IPHONE -> "iPhone XS";
                    case LAPTOP -> "Laptop";
                };
    }
}

Now go back and add a new type of device, such as WATCH.

    GAME_BOY, MACBOOK, IPHONE, LAPTOP, WATCH;

The compiler will flag your switch statement as incorrect. You will see a message like:

the switch expression does not cover all possible input values

Add the additional case in your switch, thereby satisfying the compiler. Then sleep well knowing a nasty bug was averted.

                    case WATCH -> "Watch";

Example usage:

    // Example usage.
    public static void main ( String[] args )
    {
        for ( Device device : Device.values() )
        {
            System.out.println( "device = " + device );
        }
    }
Related