Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

enums

word

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.

like image 645
Aikanáro Avatar asked Mar 15 '12 01:03

Aikanáro


People also ask

Can a class have multiple enums?

java file may have only one public class. You can therefore declare only one public enum in a . java file. You may declare any number of package-private enums.

Can enum have two values?

The Enum constructor can accept multiple values.

How many values can enum hold?

An ENUM column can have a maximum of 65,535 distinct elements.

Can we have an enum within an enum?

yes, but you need to know that.


1 Answers

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
}
like image 67
Óscar López Avatar answered Sep 28 '22 11:09

Óscar López