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

3

12

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.

Incredulous answered 15/3, 2012 at 1:57 Comment(0)
P
23

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
}
Puberulent answered 15/3, 2012 at 2:0 Comment(1)
Since emums are handled internally, I think a just will use a underscoreReticular
H
9

It is not possible. It is possible, however, to use an underscore (Science_Fiction) in the name. You can also override the toString method to return whatever you want it to (since it appears you are going for a human-readable name for your enums):

public enum Genero {
    ACTION("Action"), COMEDY("Comedy"), DRAMA("Drama"), COMPUTERS("Computers"), NOVEL("Novel"), SCIENCE_FICTION("Science Fiction");

    private final String toString;

    private Genero(String toString) {
         this.toString = toString;
    }

    public String toString(){
         return toString;
    }
}
Hornsby answered 15/3, 2012 at 2:0 Comment(4)
@TomHawtin-tackline Whoopsies, fixed now.Hornsby
I just want to assign to the Gender attribute's Book one gender. Like: Gender = Genero.ACTION I don't see where could I use the constructor or the toString method :SReticular
@Incredulous You don't have to construct your own enum objects; they're made when the class loads. If you do it this way, the variables containing the objects will be all caps and with underscores (since variable names can't have spaces), but if the actual enum objects are converted to strings, they'll have title-case names and spaces.Younker
It's a good example of constructors in enums, but since they are enums, you should definitely get rid of the setters. All fields must by a fixed valeu in a enum. You can, however, keep the getters in order for it to be accessible in other classes.Sunstroke
S
0

This might be what you want:

static private enum EnumExample { 
  R("Sample emun with spaces"),  
  G("Science Fiction");  
  final private String value; 
  EnumExample(String s) { 
    value = s; 
  } 
} 

System.out.println(EnumExample.G.value); 
System.out.println(EnumExample.valueOf("G").value); 

Science Fiction 
Science Fiction
Showman answered 15/3, 2012 at 2:6 Comment(4)
I'd make that field final and private.Shirlyshiroma
@TomHawtin-tackline Do you mean "final private String value" or ?Showman
Yes. Currently you can write EnumExample.G.value = "Comedy";.Shirlyshiroma
I would make the class nom static, the field private (nom static either) and I would create a getter for this field (but not a setter).Sunstroke

© 2022 - 2024 — McMap. All rights reserved.