Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting enums in Java

Tags:

java

enums

I want to nest some enums. The object i'm representing are Flags, with a type, and a value. There are a discrete number of types, and each type has a distinct set of possible values.

So if Type A can have values 1, 2 or 3, and Type B can have values 4,5,6, I'd like to be able to do things like:

Flag f = Flag.A.1;

f.getType() - returns "A"

f.getValue() - returns "1"

Flag f2 = Flag.A.4; -- Syntax error.

I'm driving myself crazy trying to nest enums within enums - is what i'm trying possible - do I need to ditch enums altogether and handcraft a static class with static members?

My best effort so far is:

public class Flag {

    enum A extends Flag {
        ONE("ONE"),
        TWO("TWO"),
        THREE("THREE");

        private A(String value) {
            Flag.type = "A";
            Flag.value = value;
        }
    }

        private static String type;
        private static String value;
}

But if I do:

Flag f = Flag.A.ONE;

The types are incompatible.

like image 681
PaulJWilliams Avatar asked Jun 21 '11 11:06

PaulJWilliams


People also ask

What is nested enum in Java?

Enums can be defined as members of a class aka 'nested enum types'. Nested enum defined as member of a class. //In file Employee.java. public class Employee { enum Department {

Can enums be nested?

So the first thing that I thought was using an Enum to store all the naming there and easily access them. Unfortunately, it turned out that Enums are actually not that flexible. Meaning that you can't have nested or multi-level Enums.

Should enums be in a separate file Java?

You don't have to declare an enum in a separate file.

How do you create multiple enums in Java?

we should do the following steps to have an enum with different values: Create enum constructor which accepts multiple values. Assign each constructor argument to a member field in the enum definition. Create getter methods so we can access any of the values assigned to a particular enum constant.


2 Answers

You cannot have a number as an enum. It has to be an identifier.

You can do this

interface Flag {
    String getType();
    int getValue();
    enum A implements Flag{
        one, two, three;
        String getType() { return getClass().getSimpleName(); }
        int getvalue() { return ordinal()+1; }
    }
    enum B implements Flag{
        four, five, six;
        String getType() { return getClass().getSimpleName(); }
        int getvalue() { return ordinal()+4; }
    }
}

Flag f = Flag.A.one;

However a simpler option may be

enum Flag {
    A1, A2, A3, B4, B5, B6;
    public String getType() { return name().substring(0,1); }
    public int getValue() { return name().charAt(1) - '0'; }
}

Flag f = Flag.A1;
like image 108
Peter Lawrey Avatar answered Sep 18 '22 10:09

Peter Lawrey


Nesting enums is not possible. But enums can implement interfaces. Why not have A and B as two different enums that both implement a TypedEnum interface with getType() and getValue() methods?

like image 27
Michael Borgwardt Avatar answered Sep 18 '22 10:09

Michael Borgwardt