Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

enums

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 (i.e. "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.

like image 645
Robottinosino Avatar asked Sep 13 '12 17:09

Robottinosino


2 Answers

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

like image 20
Vladimir Petrakovich Avatar answered Sep 28 '22 04:09

Vladimir Petrakovich


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'!";
  }
}
like image 56
Brian Avatar answered Sep 28 '22 04:09

Brian