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?
java file may have only one public class. You can therefore declare only one public enum in a . java file. You may declare any number of package-private enums.
Yes, we can define an enumeration inside a class.
We can have a nested enum type declaration inside a class, an interface, or another enum type. Nested enum types are implicitly static.
An enum is a class and follows same regulations. Having it on its own file is exactly like moving an inner class in a separate file, nothing more nor less. So yes you can move it inside one of the class and be able to access it from outside with OuterClass.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With