Are varargs allowed in the enum jum constructor?

enum MyEnum { A( 1, 2, 3, 4), B(1, 2), C(4, 5, 8, 8, 9); private MyEnum( int firstInt, int... otherInts ) { // do something with arguments, perhaps initialize a List } } 

Are there any problems with this? Any reasons not to do this?

+3
source share
2 answers

Of course, this is completely legal. There is no reason not to do this if your program requires it.

+4
source

it works. You must try

 private MyEnum(int... Ints ) 

With enumerations, you must make sure that you access them the way they are initialized. A lot of time access is all it takes

 MyEnum bob = MyEnum.A; 
0
source

All Articles