Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:51:13+00:00 2026-05-26T06:51:13+00:00

Why is it that when I define an enum, I pass it a list

  • 0

Why is it that when I define an enum, I pass it a list of field names, and then somehow those field names (e.g. Days.MONDAY) end up referring to field values? I can pass along a field (e.g. Days.MONDAY) and then use a switch to get the field value. Even more strange, when I declare the enum fields, I don’t even have to put the names in quotes, even though they are actually values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:51:14+00:00Added an answer on May 26, 2026 at 6:51 am

    Think of the Java enum as a nice syntax for defining a class. Here is a shell transcript that might help:

    $ cat > Direction.java
    public enum Direction {NORTH, EAST, SOUTH, WEST}
    $ javac Direction.java 
    $ javap Direction
    Compiled from "Direction.java"
    public final class Direction extends java.lang.Enum{
        public static final Direction NORTH;
        public static final Direction EAST;
        public static final Direction SOUTH;
        public static final Direction WEST;
        public static Direction[] values();
        public static Direction valueOf(java.lang.String);
        static {};
    }
    

    So yes, you can say EAST is a field of the class Direction and its value is an instance of class Direction. We refer to this value as Direction.EAST the same way you refer to a value through a static field of any other class.

    Perhaps the confusion is a result of the fact that you don’t see a declaration like

    public static final Direction.EAST = SOMETHING_OR_OTHER_HERE;
    

    This is understandable. Enums are designed to initialize these field values, but you don’t have to explicitly initialize them yourself. It is as if you said

    public static final Direction.EAST = new Direction();
    

    which is, as a matter of fact, a part of what is called the “Typesafe Enum” pattern which was commonly used before Java got the current enum syntax.

    Regarding your comment about quotes, there is simply no reason to have to quote anything, because there are no strings involved.

    Regarding your comment about switches, yes, it is somewhat interesting how the notion of field name and field value play out here. But that is only because the value looks like the field. In other words, the value of field EAST in Direction is, well, Direction.EAST. If you think about it, though, it is not too different from literals. What is the value of Float.NaN you may ask? Well, it is…. Float.NaN. Kind of an opaque value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a handful of helper methods that convert enum values into a list
I have the following entities AddressType is simply an enum field that define if
In Objective C, when you define an enum, all of the enum values are
I have some legacy code that contains this typedef: typedef enum simple_op_enum { #define
In C#, I am using a library that defines an enum. I would like
I have a class that defines its own enum like this: public class Test
How do I access an enum that is defined within a COM interface? Specifically,
I have a union that is defined like this: typedef union { enum {
I know that #define s, etc. are normally never indented. Why? I'm working in
In my database I have tables that define types for example Table: Publication Types

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.