Is it possible to have an enum class with enums of two or more words?

Aikanáro picture Aikanáro · Mar 15, 2012 · Viewed 10.9k times · Source

I have to choose from several types of genres for books and I was thinking using enums for this, but there are several genres composed by two or more words like "Medical, Health & Fitness", "Art & Photography", "Science Fiction", etc.

public enum Genero {
    Action, Comedy, Drama, Computers, Novel, Science Fiction
}

But I got a syntax error for "Science Fiction". I tried putting it with double quotes and simple quoutes, but neither worked. This enum is going to be use as a attribute for Book class.

Answer

Óscar López picture Óscar López · Mar 15, 2012

No, it's not possible. Enum names must be valid Java identifiers - that means, no spaces. The usual convention is to declare enum names in all upper-case characters and separate words using an underscore, like this:

public enum Genero {
    ACTION, COMEDY, DRAMA, COMPUTERS, NOVEL, SCIENCE_FICTION
}