prakashjv
prakashjv

Reputation: 339

Any alternative to extending an enum in java?

I am in a situation where I have an enum which has grown huge and gotten out of control on maintenance. I wanted to delegate responsibility to each class. However, there are certain common ones which qualifies as required in more than one class. I was thinking of defining a parent enum with necessary common ones defined and then extend parent enum by respective class's enums to add class specific entry. Java does not support extending enum. Does anyone know an better alternative?

I appreciate any suggestions provided. Thanks!

Upvotes: 2

Views: 862

Answers (2)

lodo
lodo

Reputation: 2383

You should maybe switch to integers, like lots of JavaSE APIs do.

class Constants
{
    public static final int COMMON_CONSTANT_1 = 1;
    public static final int COMMON_CONSTANT_2 = 2;
    ....
    static final int END_OF_COMMON_CONSTANTS = 127;
}

class AClass
{
    public static final int CONSTANT_NEEDED_BY_ACLASS = 
         Constants.END_OF_COMMON_CONSTANTS + 1;
    public static final int ANOTHER_CONSTANT =
         CONSTANT_NEEDED_BY_ACLASS + 1;
    ....
}

This way any class can use the common constants and extend them with custom ones, without interfering with each other.

Upvotes: 1

Andrea Cip
Andrea Cip

Reputation: 41

If your enumeration has only one property , you can move around in a file.properties , otherwise you may create inner enum class in a class to have everything neater

Upvotes: 0

Related Questions