1

I wanted to know what is the best way to use a final variable inside an enum?, I tried that but i get the following error: Illegal forward reference.

enum KeyTypes {

    BOLSA(NONE), // Illegal forward reference

    LLAVE(NONE), // Illegal forward reference

    MAGIC("net.labs.key.magical");

    private static final String NONE = "";

    private final String keyClass;

    KeyTypes(String keyClass).....

}
Romeo
  • 45
  • 4

2 Answers2

3

I suggest this :

enum KeyTypes {

    BOLSA(), // default constructor

    LLAVE(), // default constructor

    MAGIC("net.labs.key.magical");

    private static final String NONE = "";

    private final String keyClass;

    KeyTypes() {
        this(NONE);
    } 

    KeyTypes(String keyClass).....

} 
daniu
  • 14,137
  • 4
  • 32
  • 53
2

Assign the property with reference instead of direct property name:

public enum KeyTypes {

    BOLSA(KeyTypes.NONE),   // assign value with reference here
    LLAVE(KeyTypes.NONE), 
    MAGIC("net.labs.key.magical");

    private static final String NONE = "";
    private final String keyClass;

    KeyTypes(String keyClass){
        this.keyClass=keyClass;
    }

}
Jimmy
  • 995
  • 9
  • 18