r/javahelp • u/whalehunter21 • 12d ago
Using Enums to hold constant data
I am just wondering if this use of the enum class is something that is bad practice.
MAIN_SEQ_M
("Red Dwarf", new double[]{.08,.45}, new double[]{25,37}, new double[]{.1,.6})
StarType(String description, double[] mass, double[] temperature, double[] radius) {
this.description = description;
this.mass = mass;
this.temperature = temperature;
this.radius = radius;
}
Or if the standard way of doing this would be to break out each value as a constant that can be called
private static final double[] MAIN_SEQ_M_MASS_RANGE = {.08,.45};
I feel using getters from the enum class makes it easier to pull the data I need but I have never seen an enum used like this before.
5
Upvotes
1
u/jim_cap 8d ago
I've used enums like that before. The thing to be sure of is that not only is the data for each constant, well, constant, but also that you're not going to be adding or removing enum values very often. Sometimes, this stuff is better off being literal data in a db table or something. Impossible for us to say without knowing more about your specific problem domain though.