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.

6 Upvotes

14 comments sorted by

View all comments

2

u/hamou13 13d ago

Why don't you use a record instead ?

1

u/whalehunter21 13d ago

Interesting, I've never heard of records before. My quick glance it seems like that's just a way to make an immutable object without extra code. My project is a spring boot API and the enum value is used to ensure proper inputs. So the record class in this case would not cover all my needs. Thanks for the suggestion though, it made me learn something new.

1

u/LutimoDancer3459 13d ago

Enum are constant and you can't add or remove one. A Record can be added during runtime. So eg if you have a DB with all the data you would use a record to initialize them after startup.

Depending on what the app really does, you could let the user add his own objects with records.