简体   繁体   中英

How to initialise a Java enum using an inner static final field?

I am designing a text-only videogame with two characters not often seen together, yet very much alike in heart and disposition.

My problem is that I don't know how to initialise an enum constant through a constructor using a static final inner constant. Otherwise the game is good to go. ;)

Here's the dilemma:

  1. The enum constants must be defined in the first line of the enum, if I am not mistaken
  2. The first line can't refer to anything coming after it (ie "cannot reference a field before it is defined")

How do I resolve this catch-22?

Here some sample code released from the game under non-disclosure agreement:

enum ValiantHeroWithPrincessSavingTendencies {

  SUPERMARIO(TYPICAL_QUOTE_FROM_MARIO), ZELDA(TYPICAL_QUOTE_FROM_ZELDA);

  private String aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;

  public String getQuoteUnderStressfulCircumstances() {
    return aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;
  }

  private ValiantHeroWithPrincessSavingTendencies(String quote) {
    aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive = quote;
  }

  private static final String TYPICAL_QUOTE_FROM_ZELDA = "Have at ya!";
  private static final String TYPICAL_QUOTE_FROM_MARIO = "We, wagliu'!";
}

I am trying to initialise SUPERMARIO using TYPICAL_QUOTE_FROM_MARIO but I haven't defined TYPICAL_QUOTE_FROM_MARIO yet. Moving the private static final field before SUPERMARIO is illegal, I think.

The only viable options are to either a) move your constants to another class or b) just put your constants directly into the value initializers.

If you move your constants, you can make the class a static class in the enum:

enum ValiantHeroWithPrincessSavingTendencies {
  SUPERMARIO(Quotes.TYPICAL_QUOTE_FROM_MARIO),
  ZELDA(Quotes.TYPICAL_QUOTE_FROM_ZELDA);

  private String aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;

  public String getQuoteUnderStressfulCircumstances() {
    return aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;
  }

  private ValiantHeroWithPrincessSavingTendencies(String quote) {
    aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive = quote;
  }

  private static class Quotes {
    private static final String TYPICAL_QUOTE_FROM_ZELDA = "Have at ya!";
    private static final String TYPICAL_QUOTE_FROM_MARIO = "We, wagliu'!";
  }
}

You can just access them via class name:

enum ValiantHeroWithPrincessSavingTendencies {
    SUPERMARIO(ValiantHeroWithPrincessSavingTendencies.TYPICAL_QUOTE_FROM_MARIO),
    ZELDA(ValiantHeroWithPrincessSavingTendencies.TYPICAL_QUOTE_FROM_ZELDA);

    ...

    private static final String TYPICAL_QUOTE_FROM_ZELDA = "Have at ya!";
    private static final String TYPICAL_QUOTE_FROM_MARIO = "We, wagliu'!";
}

It's simplier than Brian's solution

The private static final constants are local to the enum; just code them in the instance definitions. After that point they can be accessed internally from the aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive variable.

You could always do something hacky like this:

public enum Derp
{
    SOMETHING(),
    SOMETHINGELSE();

    private String herp;

    public static final String A = "derp", B = "derp2";

    public String getHerp()
    {
        return herp;
    }

    static
    {
        SOMETHING.herp = A;
        SOMETHINGELSE.herp = B;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM