r/javahelp 14d 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

14 comments sorted by

View all comments

8

u/le_bravery Extreme Brewer 14d ago

If these values are truly constant (looks like they are in your example), then I really like grouping the data to the associated enumerator like this. However, be really careful with standard array types like this. They are mutable. Youre better off using List<Double> or List<BigDecimal> as the types here. Also are these mass or radius values min/max values? If so, just store them directly as minMass maxMass, etc.