I don't have that much experience in Java compared to .NET. In .NET, enums are treated as thin wrappers over integers, so you can easily create an enum value that is unnamed. For example:
// C# code
public enum Colors { Red, Green, Blue }
Console.Writeline(Colors.Red + " " + Colors.Green + " " + Colors.Blue); // Red Green Blue
var unknown = (Colors)(-1);
Console.WriteLine(unknown); // -1
Is it possible to do the same thing in Java?
edit: This seems to be the case from the fact that this code won't compile:
// Java code
enum Colors { R, G, B }
static int f(Colors c) {
switch (c) {
case R: return 1;
case G: return 2;
case B: return 3;
} // Compiler complains about a missing return statement
}
default:case where I throw an exception every time I switch on an enum, even if I've taken care of all the defined values. I'm wondering whether I have to do the same thing for Java.