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

  • Home
  • SEARCH
  • 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 7937199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:27:44+00:00 2026-06-03T22:27:44+00:00

enum Color {RED, GREEN, BLUE}; class SwitchEnum { public static void main(String[] args) {

  • 0
enum Color {RED, GREEN, BLUE};
class SwitchEnum
{
  public static void main(String[] args)
  {
    Color c = Color.GREEN;
    switch(c)
    {
      case RED:
        System.out.println("red");
        break;
      case GREEN:
        System.out.println("green");
        break;
      case BLUE:
        System.out.println("blue");
        break;
    }
  }
}

The above code compiles fine and gives the expected output.

My question is why when creating the Color reference ‘c’ we needed to refer it through the name of the enum (i.e. Color.GREEN) but in the case block only the enum value sufficed. Shouldn’t it have been

case Color.RED:

etc???

  • 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-06-03T22:27:46+00:00Added an answer on June 3, 2026 at 10:27 pm

    No, it shouldn’t. The Java compiler is smart enough to know that you are switching on a Color and so the language allows for this shortcut (and as Paul notes, requires it). In fact, the whole compilation of a switch statement depends on the compiler knowing what you’re switching on, since it translates the switch into a jump table based on the index of the enum value you specify. Only very recently have you been able to switch on non-numerical things like a String.

    The relevant part of the language spec is in JLS Chapter 14.11:

    ...
    SwitchLabel:
       case ConstantExpression :
       case EnumConstantName :
       default :
    
    EnumConstantName:
       Identifier
    

    If you’re looking for insight into why the language was designed the way it was, that’s going to be hard to answer objectively. Language design is nuanced, and you have to consider that the assignment syntax was written years and years before enum support was added.

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

Sidebar

Related Questions

I have an enum namespace Business { public enum Color { Red,Green,Blue } }
I have an enum: public enum Color { Red, Blue, Green, } Now if
I have an enum string name; public enum Color { Red, Green, Yellow }
Can we use enum inside a switch? public enum Color { RED,BLUE,YELLOW } public
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
I've a Color Enum public enum color { GREEN, WHITE, RED } and I
I have an enum public enum Color { Red = 0, Blue = 1,
static void Main(string[] args) { List<string> tests = new List<string>() { TypeTester.NonExist, TypeTester.Thing, TypeTester.Fruit,
I have an enumeration: public enum MyColours { Red, Green, Blue, Yellow, Fuchsia, Aqua,
When I declare a enum variable like this: enum paint_colors { RED, GREEN, BLUE,

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.