Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private static final variable inside an enum

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) {
      ...
    } 
  }
 ...
}
like image 922
will Avatar asked Feb 14 '12 00:02

will


People also ask

Can enum have private variables?

Enum FieldsThe enum constructor must be private . You cannot use public or protected constructors for a Java enum . If you do not specify an access modifier the enum constructor it will be implicitly private .

Can you have a private static variable?

Just like an instance variables can be private or public, static variables can also be private or public.

Are enums static and final?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

What is a private static final variable?

private static final will be considered as constant and the constant can be accessed within this class only. Since, the keyword static included, the value will be constant for all the objects of the class. private final variable value will be like constant per object. You can refer the java.


3 Answers

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"; } 
like image 73
Perception Avatar answered Sep 21 '22 17:09

Perception


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";  
  }
 ...
}
like image 41
Jug6ernaut Avatar answered Sep 21 '22 17:09

Jug6ernaut


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) {
      ...
    } 
  }
 ...
}
like image 35
tig Avatar answered Sep 18 '22 17:09

tig