Enum values().length vs private field

Viewed 64118

I have enumeration like this:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    private Configuration(int id) {
        this.id = id;
    }
    public int getId() { return id; }
}

Sometimes I need to check how many fields I have in enumeration. What is the best solution? Should I use a method "values().length"? Or maybe, I must create constant field in enumeration like this:

public enum Configuration {
    XML(1),
    XSLT(10),
    TXT(100),
    HTML(2),
    DB(20);

    private final int id;
    private Configuration(int id) {
        this.id = id;
    }
    public int getId() { return id; }

    public static final int Size = 5;
}

What is the fastest and more elegant solution?

4 Answers
Related