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
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:
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)
The problem should go away if u r using JDK1.7 .Try following the below steps and see
If u do below , then the problem reappears.
Here's what I see if I do the following...
Hope this helps!!
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