enum is just syntactic sugar for a private constructor class, the only instances of which are stored in public static final fields. Having defined this manually, you can get additional flexibility, for example, the ability to create subclasses and implement interfaces. So, one of the possible solutions, but each of them requires one class, so think carefully before deciding to do this:
public interface Country { static final France FRANCE = France.INSTANCE; static final Norway NORWAY = Norway.INSTANCE; static final Sweden SWEDEN = Sweden.INSTANCE; static final Denmark DENMARK = Denmark.INSTANCE; ... } public interface EuropeanUnionCountry extends Country { static final France FRANCE = France.INSTANCE; static final Sweden SWEDEN = Sweden.INSTANCE; static final Denmark DENMARK = Denmark.INSTANCE; ... } public interface ScandinavianCountry extends Country { static final Norway NORWAY = Norway.INSTANCE; static final Sweden SWEDEN = Sweden.INSTANCE; static final Denmark DENMARK = Denmark.INSTANCE; }
The advantage of this solution is that you now have complete type safety: Country.NORWAY can be passed to a method that accepts a Country or ScandinavianCountry , but not one that accepts a EuropeanUnionCountry . In addition, Country.NORWAY == ScandinavianCountry.NORWAY . Please note that it would be enough to list all the countries in the Country , but repeating the corresponding sub-interfaces can make it easier to keep track of which category belongs to.
You can do this using only classes, but then you are limited by a tree hierarchy, thereby excluding, for example, those that have both the European Union and Scandinavia (since they only partially overlap).
source share