How to define static constants in a Java enum?

70,864

Solution 1

As IntelliJ IDEA suggest when extracting constant - make static nested class. This approach works:

@RequiredArgsConstructor
public enum MyEnum {
    BAR1(Constants.BAR_VALUE),
    FOO("Foo"),
    BAR2(Constants.BAR_VALUE),
    ...,
    BARn(Constants.BAR_VALUE);



    @Getter
    private final String value;

    private static class Constants {
        public static final String BAR_VALUE = "BAR";
    }
}

Solution 2

public enum MyEnum {
    BAR1(MyEnum.BAR_VALUE);

    public static final String BAR_VALUE = "Bar";

works fine

Solution 3

public enum MyEnum {
//  BAR1(       foo),   // error: illegal forward reference
//  BAR2(MyEnum.foo2),  // error: illegal forward reference
    BAR3(MyEnum.foo);   // no error

  public static final int foo =0;
  public static       int foo2=0;
  MyEnum(int i) {}

  public static void main(String[] args) { System.out.println("ok");}
}

This can be done without an inner class for the constant.

Share:
70,864
jilt3d
Author by

jilt3d

Updated on July 05, 2022

Comments

  • jilt3d
    jilt3d almost 2 years

    Is there any way to define static final variables (effectively constants) in a Java enum declaration?

    What I want is to define in one place the string literal value for the BAR(1...n) values:

    @RequiredArgsConstructor
    public enum MyEnum {
        BAR1(BAR_VALUE),
        FOO("Foo"),
        BAR2(BAR_VALUE),
        ...,
        BARn(BAR_VALUE);
    
        private static final String BAR_VALUE = "Bar";
    
        @Getter
        private final String value;
    }
    

    I got the following error message for the code above: Cannot reference a field before it is defined.

  • DaShaun
    DaShaun about 7 years
    I think it should be noted that the @RequiredArgsConstructor is from the lombok library.
  • Bruno Leite
    Bruno Leite almost 7 years
    Although this one does not working for using the 'constants' into annotations values.
  • BeUndead
    BeUndead about 6 years
    @BrunoLeite This does work for using the constants in annotation values if you make the Constants class non- private.
  • Snickbrack
    Snickbrack over 4 years
    I don't see any aspect that this answer adds to the existing and accepted answer. Could you please clarify your answer any further by providing a explanation?
  • user207421
    user207421 over 4 years
    'Static inner' is a contradiction in terms.
  • user207421
    user207421 over 4 years
    @Snickbrack I do. It eliminates the static nested class. Good answer.
  • Maciej Dobrowolski
    Maciej Dobrowolski over 4 years
    @user207421 Static nested it is. Thanks :)
  • borjab
    borjab about 4 years
    Could the upper case syntax cause confusion between enum values and constants?
  • Bentaye
    Bentaye about 4 years
    @borjab yes if you use that method, you can't have a constant and an enum with the same name
  • gillesB
    gillesB almost 4 years
    Do you have an explanation why this works? I am wondering why explicitly using MyEnum makes a difference, because implicitly it is already there.
  • raspacorp
    raspacorp over 3 years
    Tried this and gives the same error: illegal forward reference in compilation time
  • bit_cracker007
    bit_cracker007 almost 3 years
    Should fail during runtime!
  • Guilherme Taffarel Bergamin
    Guilherme Taffarel Bergamin almost 3 years
    @bit_cracker007, I've just tried this and it works. Great for using in annotations. At first thought, seems better than the accepted answer
  • Guilherme Taffarel Bergamin
    Guilherme Taffarel Bergamin almost 3 years
    @gillesB, if you don't use MyEnum, it gives you an Illegal forward reference error. I think that you need to call the constant as if from another place because if not, in practice you would be using an attribute from an object that doesn't exist yet, so you need to statically access that value "from outside".