Private static final variable inside an enum

23,968

Solution 1

The enum constants need to be the first elements in the Enumeration. A version of your code that compiles:

class Foo {

    public enum MyEnum {
        MyEnumType, MyEnumType2;

        public String bar() {
            return MY_STRING;
        }

        public String bar2() {
            return MY_STRING + "2";
        }

        private static final String MY_STRING = "a string I reuse in the enum";
    }
}

* EDIT *

Based off edits made to your original question, I see what you are trying to do. No its not possible to use constants in your enum literal declarations that are declared in the enum definition itself. This is because the literal declarations MUST be the first elements in the enum. This is mandated by the Java Language Specification. Two quick things though:

  1. You are using private static final Strings. This gives you absolutely no benefit whatsoever over using string literals instead, which would solve the problem.
  2. If you wanted to declare reusable constants (public static final Strings) then you would need to do so outside of the enum.

Alternatively you can declare your Enums as nested elements of a class which defines the private static final String's for you.

Some pseudocode:

public class Foo {
    public static enum MyEnum {
        MyEnumType(0, MY_STRING), MyEnumType2(1, "Hello");

        private int ordinal;
        private String value;

        MyEnum(int ordinal, String value) {
            this.ordinal = ordinal;
            this.value = value;
        }

        public int getOrdinal() {
            return ordinal;
        }

        public String getValue() {
            return value;
        }

        public void setOrdinal(int ordinal) {
            this.ordinal = ordinal;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    private static final String MY_STRING = "a string I reuse in the enum";
}

Solution 2

It is possible, you just have to directly reference the variables.

class Foo {
  ...

  public enum MyEnum {

    MyEnumType(1, MyEnum.MY_STRING),
    MyEnumType2(2, MyEnum.MY_STRING),
    MyEnumType3(3, MyEnum.MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    }

     private static final String MY_STRING = "a string I use in a constructor";
     private static final String MY_OTHER_STRING = "a string I use in another constructor";  
  }
 ...
}

Solution 3

Interface can be used:

class Foo {
  ...

  private interface MyEnumConstants {
    static final String MY_STRING = "a string I use in a constructor";
    static final String MY_OTHER_STRING = "a string I use in another constructor";      
  }

  public enum MyEnum implements MyEnumConstants {
    MyEnumType(1, MY_STRING),
    MyEnumType2(2, MY_STRING),
    MyEnumType3(3, MY_OTHER_STRING);

    MyEnum(int num, String str) {
      ...
    } 
  }
 ...
}

Solution 4

You can declare interface within enum like this

enum MyEnum {
    MyEnumType(1, EnumConstants.MY_STRING), 
    MyEnumType2(2, EnumConstants.MY_STRING), 
    MyEnumType3(3, EnumConstants.MY_OTHER_STRING);

    MyEnum(int num, String str) {

    }

    interface EnumConstants {
        static final String MY_STRING = "a string I use in a constructor";
        static final String MY_OTHER_STRING = "a string I use in another constructor";
    }
}
Share:
23,968
will
Author by

will

Updated on December 28, 2020

Comments

  • will
    will over 3 years

    I'm trying to create a private static final variable inside of an enum, but I keep getting a compile error. Does anyone know how to fix this?

    Multiple markers at this line

    • Syntax error, insert "Identifier" to complete EnumConstantHeaderName
    • Syntax error, insert "}" to complete EnumBody
    class Foo {
      ...
    
      public enum MyEnum {
        private static final String MY_STRING = "a string I use in a constructor";
        private static final String MY_OTHER_STRING = "a string I use in another constructor";      
    
        MyEnumType(1, MY_STRING),
        MyEnumType2(2, MY_STRING),
        MyEnumType3(3, MY_OTHER_STRING);
    
        MyEnum(int num, String str) {
          ...
        } 
      }
     ...
    }