finner

finner

Enum: Behind the scenes

I’ve never really felt 100% comfortable using the enum type because of my lack of understanding how it is constructed . . .

. . . until now :grin:

Here is how a basic enum might be typically declared.

enum DevTalkForum {
   NEWS,
   CHAT,
   JOURNALS,
   BOOK_ERRATA
}

And here is the class that is constructed behinds the scenes:

final class DevTalkForum extends Enum<DevTalkForum> {

   /**
    *   the type Enum only has 2 simple properties: 
    */
   private DevTalkForum(String name, int ordinal) { 
      super(name, ordinal); 
   }

   /**
    *   each element in the enum is a subclass of the type Enum
    */
   public static final DevTalkForum NEWS = new DevTalkForum("NEWS", 0);
   public static final DevTalkForum CHAT = new DevTalkForum("CHAT", 1);
   public static final DevTalkForum JOURNALS = new DevTalkForum("JOURNALS", 2);
   public static final DevTalkForum BOOK_ERRATA = new DevTalkForum("BOOK_ERRATA", 3);

   /**
     *   all the enum classes are stored in an array
     */
   private static final DevTalkForum[] VALUES = { NEWS, CHAT, JOURNALS, BOOK_ERRATA };
   
   public static DevTalkForum[] values() { 
      return VALUES.clone(); 
   }
   
   public static DevTalkForum valueOf(String name) {
      for (DevTalkForum e : VALUES) 
         if (e.name().equals(name)) return e;
      throw new IllegalArgumentException();
   }
}

Realising how the enum is constructed has helped me understand how to use it correctly.
For example, the toString() of the class can be overridden to provide an alternative string value if you do not want to add another property.

enum DevTalkForum {
    NEWS,
    CHAT,
    JOURNALS,
    BOOK_ERRATA;

    @Override
    public String toString() {
        switch (this) {
            case CHAT:
                return "Chat";
            case JOURNALS:
                return "Journals";
            case BOOK_ERRATA:
                return "Book Errata";
            case NEWS:
            default:
                return "News";
            
        }
    }
}

So now that I feel closer to the enum I think I’m going to try and pop them into my code a bit more often.

Hope this was somehow helpful

Most Liked

AstonJ

AstonJ

Nice one Finner :+1:

Funnily enough, I had to re-read the Enumerable and Enumerator chapter in The Well Grounded Rubyist (which I loved btw) a couple of times as it’s a little harder to grok than some of the other stuff. I’d actually like to read the book again one day as I loved it :smiley:

Popular Backend topics Top

PragmaticBookshelf
The next step in the evolution of user interfaces is here. Chatbots let your users interact with your service in their own natural langua...
New
PragmaticBookshelf
Learning Clojure involves much more than just learning the mechanics. To really get Clojure you need to understand the ideas underlying i...
New
DevotionGeo
How Dgraph was running out of memory for some users, and how Go’s Garbage collector wasn’t enough, and Dgraph team used jemalloc to manag...
New
andrea
Can Phoenix LiveView be used in multi-page applications, unlike React/Vue/Blazor which seems to be targeted for SPA?
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
ManningBooks
Deep Learning with Python, Second Edition is a comprehensive introduction to the field of deep learning using Python and the powerful Ker...
New
First poster: OvermindDL1
Today we are happy to announce axum: An easy to use, yet powerful, web framework designed to take full advantage of the Tokio ecosystem. ...
New
mafinar
Hello folks! We had a pretty fun thread here around the same time last year - talking about Advent of Code problems. That also happened t...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1016 16836 371
New
AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New