Using an enumeration inside a switch

Can I list inside the switch?

public enum Color { RED,BLUE,YELLOW } public class Use { Color c = Color.BLUE; public void test(){ switch(c){ case Color.BLUE: } } } 

I get some error in this.

 The enum constant Color.BLUE reference cannot be qualified in a case label Use.java line 7 Java Problem 
+4
source share
5 answers
 case COLOR.BLUE: } 

In the code above, instead of COLOR.BLUE, write only BLUE


eg.

 import java.awt.Color; class ColorEnum { enum Color{BLUE,RED,YELLOW}; public static void main(String[] args) { Color c = Color.BLUE; switch(c) { case BLUE: System.out.println("Blue!"); break; case RED: System.out.println("Red!"); break; case YELLOW: System.out.println("Yellow!"); break; default: System.out.println("Logic error!"); } } } 
+10
source

Write this:

 public void test(){ switch(c) { case BLUE: } } 

The enum tag MUST NOT be qualified when used as a body tag. In grammar JLS 14.11 reads as follows:

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

Note that a simple identifier is required, not an identifier assigned by an enumeration name.

(I donโ€™t know why they designed this syntax. Perhaps this was to avoid some ambiguity in the grammar. But in any case, the way it is.)

+4
source

Why use a switch at all? Rather, just let enum hold the color information (encapsulate it) and thereby do all the dirty work. The advantage of this is that if you change your enum, you do not need to root through all the code that uses it, changing all the switch statements. For instance:

 import java.awt.Color; public enum MyColor { RED("Red", Color.red), BLUE("Blue", Color.blue), YELLOW("Yellow", Color.yellow); private String text; private Color color; private MyColor(String text,Color color) { this.text = text; this.color = color; } public String getText() { return text; } public Color getColor() { return color; } @Override public String toString() { return text; } } 

and an example of how this can be used is as follows:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; @SuppressWarnings("serial") class MyColorTest extends JPanel { private static final Dimension PREF_SIZE = new Dimension(400, 300); public MyColorTest() { for (final MyColor myColor : MyColor.values()) { add(new JButton(new AbstractAction(myColor.getText()) { @Override public void actionPerformed(ActionEvent arg0) { MyColorTest.this.setBackground(myColor.getColor()); } })); } } @Override public Dimension getPreferredSize() { return PREF_SIZE; } private static void createAndShowUI() { JFrame frame = new JFrame("MyColorTest"); frame.getContentPane().add(new MyColorTest()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } 
+1
source

Yes, you can use enumerations in switch statements, but it is not necessary to use FQCN (fully qualified class name) in case labels.


After that, enum is extracted from the reference constant and cannot be defined in the case label of the switch statement "

In short

When the Java switch statement uses the enum parameter; qualified enumeration value names should not be used in the case of labels, but only unqualified names; then the switch statement will assume that all labels refer to the type of enumeration, which is used as a parameter.

Why only unqualified values?

If qualified labels were allowed for case labels; it would be impossible to limit the type of enumeration used in labels to the same as the parameter type of the switch statement.

 public enum Status { REGISTERED, TERMINATED } public class TestStatus { public static String getMessage(Status status) { String message; switch (status) { // case Status.REGISTERED: // line 1 case REGISTERED: // line 2 message = "Welcome"; break; default: message = "Good bye"; break; } return message; } 
0
source
 enum MyEnum{ A,B,C MyEnum(){ switch(this){ case A: //.... break; case B: //.... break; case C: //.... break; } } } 
0
source

All Articles