Enum class with type parameters

Do you have a good use-case?

The problem I see is that if you manipulate an enum member through the enum type, you can’t access the type parameter.

So you only get access to it when you manipulate the enum member by name. But then you can “harcode” the parameter everywhere it is used.

That leaves extending a generic class with different parameters for different enum members. That is indeed Lukas’ exemple:

public interface DataType<T> {}

public enum SQLDataType<T> implements DataType<T> {
    TINYINT<Byte>,
    SMALLINT<Short>,
    INT<Integer>,
    BIGINT<Long>,
    CLOB<String>,
    VARCHAR<String>,
    ...
}

But that’s only the definition. How to use that usefully?

1 Like