0

I am constructing an enum Flower which contains the fixed set of constants for Flowers as described below. I'm writing it in a separate file and importing it to other classes so that it could be used across my project.

Is it a good practice to write an enum in a separate file in Java. Can someone give me some advice on this.

public enum Flower {

    Jasmine(1),
    Lotus(2),
    Lilly(3),
    Sunflower(4),
    Tulip(5),
    Daria(6);

    private int flowerValue;

    public Flower(int value) {
         this.flowerValue = value;
    }

    public int getFlowerValue() {
         return this.flowerValue;
    }

    public String getFlowerName() {
         return this.name();
    }
}
  • 4
    FWIW, if an enum seems like it will only be used in one or two places, I declare it in the class that needs it and expose it to the other. Once it's used in > 2 classes/packages, I break it out into its own file. I started doing that when I realized I sometimes had enums that were created for a very specific purpose and were never used outside one or two classes. – FrustratedWithFormsDesigner Mar 21 '17 at 15:31
6

Yes, it absolutely is. Its the same as with usual classes and polymorphism:

// this code in approx. 100 places

int flowerValue;
switch(flower) {
    case Flower.Jasmine: flowerValue = 1; break;
    case Flower.Lotus:   flowerValue = 2; break;
    ...
    case Flower.Daria:   flowerValue = 6; break;
}

// good luck adding a new flower...

versus

// this code in approx. 100 places
int flowerValue = flower.getFlowerValue()

In this particular case it seems as if flower.ordinal() would work just as well.

  • Yep, this is how I do it. – PrometheanVigil Mar 21 '17 at 14:50
  • 1
    Beware of ordinal(), since it is dependent on the arrangement of the constants in your code. If just one colleague happens to check the box in his IDE that rearranges declarations of constant class members (enum constants for example) alphabetically, then it can break your program in a very subtle way. If you want to associate some business information with your constants, having them in explicitly defined fields with explicitly defined values is the way to go. ordinal is really just for use cases a la "I want some numerical ordering of the constants, I don't care which, just pick one" – Johannes Hahn Mar 21 '17 at 16:33
  • @JohannesHahn intereseting point! I use to declare my enum constants in a logical order; when there is business logic that requires the constants to have a natural order, i often use the order of declaration for that and put a bold line in the enums javadoc stating that the order of declaration is relevant (as that seems to be the easiest way to have the domain model reflect the business rules). Never had such a colleague, but that might happen... – marstato Mar 21 '17 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.