r/java • • Aug 03 '25

Teach Me the Craziest, Most Useful Java Features — NOT the Basic Stuff

I want to know the WILD, INSANELY PRACTICAL, "how the hell did I not know this earlier?" kind of Java stuff that only real devs who've been through production hell know.

Like I didn't know about modules recently

398 Upvotes

279 comments sorted by

View all comments

Show parent comment

13

u/shinmai_rookie Aug 03 '25

I think "obsolete" is a bit of an exaggeration haha. When you need a small amount of stateless classes with a relatively simple behaviour (for your own definitions of small and simple), enums give you less boilerplate (no need to define private constructors, or final class Whatever extends Whatever), they're all more neatly packed together, and you get autocomplete from your IDE (if applicable), not to mention EnumSets if you need them and an easier access to all the possible values without reflection (Operation.values if memory holds, but I'm a bit rusty).

4

u/Polygnom Aug 03 '25

I said "kinda" for a reason. You get a couple things with sealed types you dont get with enzms and vice versa. Biggest advantage of sealed types is that you can restrict the subset of allowed values, which you cannot with enums (there is an old JEP forcenhanced enums that would allow this, but it sees little traction).

2

u/OwnBreakfast1114 Aug 05 '25

The JEP was withdrawn because it was interacting poorly with generics. https://www.reddit.com/r/java/comments/jl3wir/jep_301_enhanced_enums_is_withdrawn/ I was also looking forward to this one, but it's not happening any time soon.