How to write multiple enum classes in a class

I have a question about writing multiple enums in a java class. Can we do this?

Could you please help me on how to do this?

For Ex:

public final class Test{

    public enum Unit{
        HORIZONTAL("HORIZONTAL");

    }

    public enum Code {
        COMPANY ("COMPANY");
    }

    public enum Version{
        ONE(1)

    }
}

      

+3


source to share


2 answers


You can indeed have multiple enums defined in a class, but each of your enums requires the constructor to deal with parameters in parentheses, i.e.

public enum Unit {
    HORIZONTAL("HORIZONTAL"), VERTICAL("VERTICAL");
    private String unit;

    Unit(String unit) {
        this.unit = unit;
    }
}

      

or



Version(int versionNumber){}

      

For further explanation, please indicate the question.

+4


source


An enum definition in Java is a class. The language provides syntactic features specific to enums, but as far as the JVM is concerned, an enumeration is a class.

In fact, the enumeration that is declared:

public enum MyConstants{ ... }

      

implemented in JVM as:

public static class Enum<MyConstants> { ... }

      

The rules for declaring enum classes are thus the same as for declaring classes. Java file can only have one public class.

This way you can only declare one public enum in a .java file. You can declare any number of packages as private enumerations.



If you have multiple enums related to each other, there is no harm in declaring them all as nested members of the enclosing class:

public class MyEnumContainer {

    static enum MyEnum1 { ... }

    static enum MyEnum2 { ... }

    :
    :
}

      

In this case, you will need to import your MyEnclosingClass or reference constants like MyEnclosingClass.MyEnum1.ENUM_ONE for example. Note that all enums are static classes, so using the static keyword does not penalize you, but it is not necessary.

Each of the enumerations of a member enum must be self-contained (i.e., must be a complete definition of an enum). If you have data associated with a constant, then each enumeration definition will need its own fields and an instance constructor. If you have methods specific to a particular constant, each enumeration will need to provide methods.

If you are worried about a lot of code duplication, you can wrap the generic code in a private static helper class and then forward method calls to those contained in your enumeration definitions.

Using enum definitions, nested enums should look something like this:

public final class Test{

    public enum Unit {
         HORIZONTAL ("HORIZONTAL"),
         VERTICAL   ("VERTICAL"),
         :
         :
         DIAGONAL   ("DIAGONAL");

         private final String e_name;

         Unit(String name)
             { this.e_name = name; }

         public String getName() { return e_name; }
    }

    public enum Code {
    :
    :
    }

    public enum Version{
    :
    :
    }
}

      

+4


source







All Articles