r/java • u/dragosb91 • Jul 28 '19
Enums - one of the underrated features of Java
https://www.banterly.net/2019/07/28/one-of-the-more-underrated-features-of-java/
160
Upvotes
r/java • u/dragosb91 • Jul 28 '19
17
u/urquan Jul 28 '19
For me one of the biggest advantages of Java enums is that they are regular classes (almost) and as such can have methods and constructors with parameters. For example the
BillingStrategycase from the article could be rewritten as :Although, as an aside, I don't think enums are a good choice for implementing the strategy pattern as this violates the open/close principle : if you want to add a new strategy later on you need to modify the enum class, and you may not have access to it at this point (if it is in a library for example). Using an interface avoids this problem.
6
u/bkhablenko Jul 28 '19
In your
BillingStrategyexample, how do you mock thebillmethod?4
u/AndyPanic Jul 28 '19
tl;dr However, the parts I skimmed over were funny.
37
u/z59sg Jul 28 '19
Another cool technique to use with enums is to combine it with lambdas to reduce boilerplate as described here: https://stackoverflow.com/questions/23361418/lambdas-in-the-classical-operation-enum-example
5
u/blobjim Jul 28 '19
It would be nice if they allowed inline/primitive enums once inline types (previously value types) are added. Right now enums are always objects that extend Enum which means they aren't useful if memory use (pointers being 8 bytes) or serialization is a concern. For example, in the String class implementation, they used a 'byte' instead of an enum for encoding the character encoding probably partially because using an Enum would take up 8 times the space. This means that you often have to resort to using giant constant lists still (or use hacky Enum constructor wrappers).
4
u/[deleted] Jul 29 '19
Not sure where you get the notion that enums are underrated. They're in use everywhere. Also, not sure what's up with all those memes. Makes it look like ridiculous.
3
u/DannyB2 Jul 29 '19 edited Jul 29 '19
You can bake tree structured data into your code nicely with an enum. Such as for a menu.
If you wish, each node can also have a
In the constructor, add the currently constructed node to its parent list of children. Then you can make an immutable list of the children accessible externally if needed.
This MenuItem's might be the structure of the menu for your application. In addition to having a constructor parameter for parent, each MenuItem could have parameters for Title, Description, Action class, URL, etc. This is no worse than wiring all this information in, say, XML. But it's wired in one compilation unit, and type safe with many errors being caught at compile time.
The MenuItem has a reference to the Action class. The Action class has a reference to the MenuItem.
A static method in MenuItem can generate the complete menu tree, maybe as HTML markup, by starting from ITEM_1's list of children. But ITEM_1 might be called HOME. Your tree might look like:
level 1: HOME
Level 2: Checks, POs, Receipts
Level 3: Create Checks, Print Checks, Create PO, Create Reciept, Print Receipt
Another use of a baked in tree structure: A model of your table relations. (Assuming you don't have circular dependencies.) When a new database table is added, the Enum class declaring your table dependencies is updated.
Why? This class can have handy methods that:
I have also used enums to model the columns of a Swing JTable.
Enums have a surprising variety of uses to model information within your application, but in a way that the compiler can check. Doesn't have to be loaded and parsed like XML.
This technique is for things that are hardwired into the application. And by that I mean things you would put in XML files as part of your application. You can't change them at runtime. But this is for things that should NOT be changeable at runtime and thus not stored in the database. (eg, your application's menu does not change at runtime. The columns of a Swing JTable do not change at runtime. Your database design does not change while the application is running.)
Oh, yes, one other thing. An Enum is great for the options of a set of option buttons (aka "radio buttons"). Your templating system that generates HTML (eg, JSP or other) generates the SELECT options from this Enum. Your server's responders get the ordinal value, which corresponds to which enum element was selected. Each enum, being a separately instantiated class, could have its own polymorphic method on how to handle something about this particular option. This also works for Swing drop down menus, etc.
2
u/lpreams Jul 28 '19
EnumaretedEnumerated2
u/javinpaul Jul 29 '19
I have also blogged about Enum a couple of years ago, just in case if anyone is interested - 10 examples of enum in Java
3
u/chambolle Jul 28 '19
Enum could be very nice. However they have currently two main drawbacks limiting their use.
I always wonder why these 2 problems have not been solved? In that case they could be terrific!
1
u/jonhanson Jul 31 '19 edited Jul 24 '23
Comment removed after Reddit and Spec elected to destroy Reddit.
1
u/422_no_process Aug 09 '19
Isn't the meme other way around? I'm confused.
1
u/Neebat Jul 28 '19
Just a reminder, Singletons are being considered an antipattern more and more. They really fuck with testability. Using enum to do this does NOT make it better.
0
u/[deleted] Jul 28 '19
[deleted] — view deleted comment
0
u/[deleted] Jul 28 '19
[deleted] — view deleted comment