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) {
...
}
}
...
}
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 .
Just like an instance variables can be private or public, static variables can also be private or public.
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).
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.
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:
private static final Strings
. This gives you absolutely no benefit whatsoever over using string literals instead, which would solve the problem.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"; }
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";
}
...
}
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) {
...
}
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With