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

New
AstonJ
Partly interested in this so we can set up tags, but also because I’m out of touch with which frameworks are hot right now and I’m curiou...
New
New
New
New
finner
Just wondering how many devs out there are using Spring Reactive, specifically WebFlux?
New
finner
I’ve never really felt 100% comfortable using the enum type because of my lack of understanding how it is constructed . . . . . . until ...
New
mafinar
So I was thinking of trying out Crystal, I had tried it multiple times but left it midway. Now that there’s a book on it and it’s version...
New
jaeyson
Hi all!, anybody tried this Elixir quiz from @Tetiana? She’s the one who made Elixircards.
New
luigiluigiluigi
Hey everyone! :wave: We’re looking for a Back-End Developer Volunteer to help us build something impactful! If you want to gain real-wor...
New

Other popular topics Top

AstonJ
Or looking forward to? :nerd_face:
New
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New
New