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:

Where Next?

Popular Backend topics Top

New
bot
Announcing the Error Handling Project Group | Inside Rust Blog. Want to follow along with Rust development? Curious how you might get in...
New
First poster: bot
The Complete AWS Lambda Handbook for Beginners (Part 1). In the first part of our Complete AWS Lambda Handbook for Beginners, we explain...
New
First poster: bot
The Emerging Architectures for Modern Data Infrastructure. Five years ago, if you were building a system, it was a result of the code yo...
New
First poster: bot
What's so exciting about Postgres? with Craig Kerstiens (The Changelog #417). PostgreSQL aficionado Craig Kerstiens joins Jerod to talk ...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
Exadra37
Finishing my app to take notes on Videos: I am aiming to put it online on my playground by this weekend. Edit: It’s up https://video...
New
mudasobwa
To promote Tarearbol.DynamicManager I created the :heart_eyes_cat:-language (which is a brainfuck dialect.) Code outputting “Meow” to th...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
AstonJ
And the blog: Rails has been unapologetically full stack since the beginning. We’ve continuously sought to include ever-more default an...
New

Other popular topics Top

New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
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