Unique enum member values
enums, java
Solution
It sounds like you shouldn't implement this as a runtime check. It can only fail if a developer who has control over the code did some bad changes. Detecting changes that introduce bugs is what tests are for.
Write a simple test that checks if everything is correct. The other answers already provide some good solutions to check for duplicated enums. Instead of running the application to see if the enum initialisation fails, run the test (I hope you already write and run tests anyway).
Problem
My problem: I want to specify a 1 to 1 connection between two enums. basically: ``` enum MyEnum{ ENUM_VALUE_1, ENUM_VALUE_2, ENUM_VALUE_3, } enum MyOtherEnum{ OTHER_ENUM_VALUE_1(MyEnum.ENUM_VALUE_3), OTHER_ENUM_VALUE_2(MyEnum.ENUM_VALUE_1), OTHER_ENUM_VALUE_3, OTHER_ENUM_VALUE_4(MyEnum.ENUM_VALUE_2), private MyEnum pair; MyOtherEnum(MyEnum pair){ this.pair=pair; } } ``` Given this code, what is the best way to make sure, that two instances of `MyOtherEnum` doesn't have the same `MyEnum` pair? I figured some solutions, but neither seem to be good. One solution is throw an exception in the constructor, but I'm not sure if that is even doable. Another solution is a special getter method, but it can be easily bypassed with simple `MyOtherEnum.values()` calls.