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

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
DevotionGeo
For me it’s the semicolon, because I stopped using a semicolon in JavaScript, two other of my favorite languages, Elixir and Go don’t hav...
New
AstonJ
Or which features of current frameworks you use you feel you couldn’t live without?
New
AstonJ
Currently a hot topic in the BEAM world, let’s start a thread for it (as suggested by @crowdhailer here) :smiley: What are your current...
New
New
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
CommunityNews
The Magic of Python Context Managers. Recipes for using and creating awesome Python context managers, that will make your code more read...
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
Jsdr3398
I really need developers to help create my messaging platform but I’m not sure how much they want etc. I’ve never hired anyone before :s...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
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
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New