C# vs. Java: Simple Enums and Crazy Enums

Although C# and Java are very similar languages, there are a few things that differ greatly. I was porting some Java code to C# and ran into Java’s crazy enum type.

The C# enum type, like most languages that support enums, is a very simple way to define a variable type which can only have discrete, categorical values. For example, a Size type might be allowed to have only values of ‘Small’, ‘Medium’, or ‘Large’. A typical C# enum example could be:

class ExperimentsProgram
  public enum Size { Small, Medium, Large };
  public enum LengthDim { Small, Large };
  public enum WidthDim { Medium, Large };

  static void Main(string[] args)

      LengthDim lengthOfSomething = LengthDim.Small;

      // etc.

But Java supports all kinds of strange enum behavior and features. For example:

public class Program {
  public enum Size { Small, Medium, Large };
  public enum Dimension {

    Length(Size.Small, Size.Large),
    Width(Size.Medium, Size.Large);
    private final Size first;
    private final Size second;
    private Dimension(Size first, Size second)
      this.first = first;
      this.second = second;
  public static void main(String[] args) {
    Size lengthOfSomething = Dimension.Length.first;
    System.out.println("Length = " + lengthOfSomething);


The Dimension enum has fields (!) and a constructor (!). If you look in the main method above, you can see that this allows a developer to create arbitrarily complex hierarchical enum types.

In general I’m a big fan of Java, but the exotic features of Java’s enum go beyond well common sense. I’ve run into crazy Java enums. I don’t like crazy Java enums. Don’t use crazy Java enums — I might have to port your code to C# at some time in the future!

This entry was posted in Miscellaneous. Bookmark the permalink.