fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public enum UniqueCodes {
  7. A(1), B(2), C(3), D(1);
  8. private static class Holder {
  9. static Set<Integer> set = new HashSet<Integer>();
  10. }
  11. private final int value;
  12. private UniqueCodes(int value) {
  13. if (!Holder.set.add(value))
  14. throw new IllegalArgumentException("Duplicate value: " + value);
  15. this.value = value;
  16. }
  17.  
  18. public int getValue() {
  19. return value;
  20. }
  21. }
  22. public static void main (String[] args) throws java.lang.Exception
  23. {
  24. System.out.println( UniqueCodes.A);
  25. }
  26. }
Runtime error #stdin #stdout #stderr 0.07s 381248KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.ExceptionInInitializerError
	at Main.main(Main.java:24)
Caused by: java.lang.IllegalArgumentException: Duplicate value: 1
	at Main$UniqueCodes.<init>(Main.java:14)
	at Main$UniqueCodes.<clinit>(Main.java:7)
	... 1 more