Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Enum referencing another enum

Tags:

java

enums

I would like to be able to define an Enum class that would include another Enum as well as its own values. For example, :

public enum A {
    A1, A2, A3;
}

public enum B{
    B1, B2, ...(somehow define references to all values in enum A)
}

such that any values contained in enum B must be either defined initself (such as B1, B2) or any values of enum A.

Thanks,

like image 804
MikeMY Avatar asked Mar 02 '11 19:03

MikeMY


1 Answers

Such that B is the union of A and B? That's not possible without B extending A, which is not supported in java. The way to proceed is to create an enum which contains the combined values of A and B, e.g.,

enum C {
    A1, A2, A3, B1, B2;
}
like image 99
Johan Sjöberg Avatar answered Oct 14 '22 07:10

Johan Sjöberg