How to initialize a Java enumeration using an internal static final field?

I am developing a text-based video game with two characters that are often not found together, but are very similar to each other in heart and location.

My problem is that I do not know how to initialize the enum constant through the constructor using a static final internal constant. Otherwise, the game is good .;)

Here's the dilemma:

  • Enumeration constants should be defined in the first line of the enumeration, if I'm not mistaken
  • The first line cannot refer to anything after it (ie "cannot refer to a field until it is defined")

How to enable this catch-22?

Here is an example of code released from a game under a 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 initialize SUPERMARIO with TYPICAL_QUOTE_FROM_MARIO, but I have not defined TYPICAL_QUOTE_FROM_MARIO yet. I think that moving a private static end field to SUPERMARIO is illegal.

+4
source share
4 answers

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

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

 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'!"; } } 
+8
source

You can simply access them through the 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'!"; } 

This is easier than Brian’s decision.

+4
source

Private static finite constants are local for enumeration; just copy them in the instance definitions. After this point, they can be obtained internally from the variable aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive .

0
source

You can always hack something 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; } } 
0
source

Source: https://habr.com/ru/post/1434050/


All Articles