• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Use of Enum constants in the Switch statement.

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the following code showing use of enum constants in Switch statement:

enum switchEnum {FIRST_CASE, SECOND_CASE, DEFAULT_CASE};

public class SwitchExample
{
private void enumSwitch()
{
switchEnum enumTest = switchEnum.SECOND_CASE;

switch(enumTest)
{
case FIRST_CASE:
System.out.println("First case");
break;
case SECOND_CASE:
System.out.println("Second case");
break;
case DEFAULT_CASE :
System.out.println("Default case");
break;
default :
System.out.println("default case");
}
}

public static void main(String[] args)
{
SwitchExample example = new SwitchExample();
example.enumSwitch();
}
}

Confusion : The above code compiles fine and runs.
But this ONLY works when using enum constants in cases as shown above.
Using cases like : "switchEnum.FIRST_CASE" doesn't compile.

Although, for assigning an enum contant to a variable the name of enum is used with the constant. But why this doesn't work in case of switch.
I am not clear why specifying the cases like "switchEnum.FIRST_CASE" is not allowed.

Thanks in advance!!
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer lies in the grammar defined for the switch statement when using an enum.

This is from the Java Language Specification, Section 14.11

SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot !!
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Java specification in the cases only enum constants are allowed , and they should not be prefixed with enum and period operator because you already provide the particular enum in "switch(enumType)" statement and that is sufficient info required by the compiler.
I guess that clears your doubt.
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anuj !!
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic