Java compilation error: switch on enum

后端 未结 3 1239
梦谈多话
梦谈多话 2021-01-20 17:54

I came across a very weird error that I just can\'t figure out how to solve.

A project, that compiles just fine on Windows, doesn\'t compile on Linux with the follow

相关标签:
3条回答
  • 2021-01-20 18:32

    I don't know whether it is windows/linux related issue. But

    From jdk5 onwards you can use enum in switch case and from jdk7 you can use String in switch case. While using enum in switch make sure that:

    1. you are using jdk5 and later
    2. All the labels used in your switch must be a valid enum object inside your enum being used in switch.

    In java enum is implemented through class concept(Each n every enum in java extends to Enum class that is an abstract class and direct sub class of Object class). So while creating enum like

     public enum Bbb {
            ONE,
            TWO;
        }
    

    It will internally meant

    public static final Bbb ONE=new Bbb();
    public static final Bbb TWO=new Bbb();
    

    Means all your defined enum objects are public, final and static objects of defined enum class. If you are using something else as switch label, it will give a compile time error.

    For each enum in java, super class Enum is final and all enum classes are internally implemented as final. So inheritance can not be used for enum in java. Means we are not allowed to use anything else in switch labels except our own class enum objects(Not even subclass objects, because enum class can't be inherited further)

    0 讨论(0)
  • 2021-01-20 18:33

    The problem should go away if u r using JDK1.7 .Try following the below steps and see

    1. Open the project properties
    2. Click on "Java Compiler"
    3. Checkmark "Enable project specific settings"
    4. Set all of the drop down menus to 1.7
    5. Hit ok
    6. Clean the project to trigger a rebuild

    If u do below , then the problem reappears.

    1. Open the project properties
    2. Click on "Java Compiler"
    3. Clear "Enable project specific settings"
    4. Hit ok
    5. Clean the project to trigger a rebuild (it will rebuild automatically)

    Here's what I see if I do the following...

    1. Open the project properties
    2. Click on "Java Compiler"
    3. Click on "Configure Workspace Settings"
    4. I see that the "Compiler compliance level" = 1.7
    5. I see that "Use default compliance settings" is checked.

    Hope this helps!!

    0 讨论(0)
  • 2021-01-20 18:54

    I have tried your code

    public class AClass {
         enum Bbb {
            ONE,
            TWO;
        }
        public void aMethod(List<Bbb> arg) {
            for (Bbb en : arg) {
                switch (en) {
    
                    case ONE: System.out.println("ONE");break;
                    case TWO: System.out.println("TWO");break;
                }
            }
        }
        public static void main(String[] args) {
    
            List<Bbb> list = new ArrayList<Bbb>();
            list.add(Bbb.ONE);
            list.add(Bbb.TWO);
    
            new AClass().aMethod(list);
        }
    }
    

    It is working fine.
    I dont know the pros and cons of passing argument like this List<Bbb> arg but atleast it is not error as much as i know in java 7

    0 讨论(0)
提交回复
热议问题