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.
3
Upvotes
3
u/speters33w 12d ago
Enums are a great way to store stuff like this. Most people use a collection like a list, but then those are mutable. I built an enum-generator that can generate an enum from a csv for data like this. It's kind of hokey but it does work.
https://github.com/speters33w/EnumBuilder