简体   繁体   中英

Are varargs allowed in a java enum 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 it?

Sure, this is perfectly legal. No reason not to do it if your program requires it.

it does work. You should try to

private MyEnum(int... Ints )

With enums you need to make sure that you access them in a manner the initializes them. A lot of the time an access is all that is needed

MyEnum bob = MyEnum.A;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM