Saturday 6 October 2007

Why I hate Java enums

Enums are nice: they are static instantiation of objects where some absolute order is introduced between the elements. However, in Java, they have made the instantiation of enums and type declaration go together. You cannot make enums of objects that you have declared previously.
From the implementation point of view the reason is obvious. If an element is a member of an enum and it has a base class, suddenly that object has multiple inheritance, a big NO-NO in Java. On the other hand, if an enum doesn't inherit from any other class the object of class T can inherit from Enum, so it is just a simple syntactical trick to make the enum. The problem is that it is worthless. If you want to create objects of that class, apart from the enumeration, you can't.
Implementing it the other way would have been harder; the element would have had to handle methods with the same name, common ancestors, etc., all the nice stuff that make C++ inheritance syntax a huge mess, and really difficult to implement. But then enums would have been useful instead of a quick gimmick for rookies; right now enums are only useful for a simple lists of Strings.
PS: I am not the only Java-enum-hater

Add to Technorati Favorites

No comments: