A DESCRIPTION OF THE REQUEST :
Today only unqualified enum constants may be used in select/case statements, and it is not permitted to use qualified enum constants. This is inconsistent with other data access alternatives in Java, and may lead to confusion.
JUSTIFICATION :
Allow the use of qualified enum constants in switch/case statements to be consistent with the ability to use qualified constants.
The enums themselves are also inconsistent in behaviour, since they currently allow for qualified access in if-statements, but not in switch/case statements.
Another reason is that enums with the same name, but with different meaning can be imported from different classes (not unheard of when integrating different software packages). In this case qualified access to enum constants is necessary.
In the test case below, the compiler complains because the case statements refer to qualified Enums. Replacing
case EnumClass.Action.ADD:
with
case ADD:
allows the test to pass.
---------- BEGIN SOURCE ----------
File EnumClass.java:
------------------------------
public class EnumClass
{
public enum Action { ADD, UPDATE, DELETE }
public EnumClass()
{
}
public Action getAction()
{
return Action.ADD;
}
}
------------------------------
File TestClass.java:
---------------------------
public class TestClass
{
public static void main(String[] args)
{
EnumClass ec=new EnumClass();
EnumClass.Action action=ec.getAction();
switch(action)
{
case EnumClass.Action.ADD:
System.out.println("Add!");
break;
case EnumClass.Action.UPDATE:
System.out.println("Update!");
break;
case EnumClass.Action.DELETE:
System.out.println("Delete!");
break;
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Two methods are possible:
Use of "import enumtest.EnumClass.Action.*;" (example above) and unqualified access to enum constants.
Use of if-statements where qualified access is permitted.
###@###.### 2004-11-06 00:38:38 GMT