19

I have 3 String arrays with constants. eg:

 String[] digit = {"one", "two", "three"};
 String[] teen= {"ten", "twenty", "thirty"};
 String[] anchors = {"hundred", "thousand", "million"};

I'm thinking of transferring these to enums separately, so I will have 3 enum classes: digit, teen and anchors with getValue methods implemented. But I don't want to have them in separate files as I have only small data and same type of data. What is the best way to have all these with access methods in same meaningful java file?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
popcoder
  • 2,952
  • 6
  • 32
  • 47

1 Answers1

59

They can be three inner classes like this:

public class Types {
  public enum Digits {...}
  public enum Teens {...}
  ....
}

Then refer them Types.Digits.ONE, Types.Teen.TWENTY etc.

You can also use static imports like this:

import Types.Digits;
import Types.Teen;

..

in order to have shorter references: Digits.ONE, Teen.TWENTY etc.

Eugene Retunsky
  • 13,009
  • 4
  • 52
  • 55
  • The only suggestion I would make is to change the signature of the enums to `public static enum`. That way, we can use a static import on the needed enums and save a bit of typing for the user. – jpm Apr 04 '12 at 19:20
  • 3
    enums are already static - you don't have to declare them as static (like interfaces). – Eugene Retunsky Apr 04 '12 at 19:25
  • 6
    Right you are. I'm sure I learned that at some point. Still, unless using the fully qualified name adds readability or clarity, I would still suggest going with the static import, though that's really a point of personal preference, I suppose. – jpm Apr 04 '12 at 19:29