How to customize java enum elements

Hi guys, I'm not sure if the title is descriptive enough. I mean creating an enum like so

public enum Test{
  ONE, TWO ,THREE
}

      

it looks like hard coded.if for some reason i need to add FOUR some specific reasons for evolution of business rules. Do I have to code it and deploy it again? doesn't allow it to select items from a file, spring config for example, or a properties file? Thanks for reading.

+2


source to share


3 answers


If the enum value clearly doesn't exist in the code, how could you use it? Test.Four

will not compile. Any code that could somehow refer to Test.Four

would be invalid and work until the file is read and new values ​​are added.



You can of course use arrays or collections of values ​​and manipulate them at runtime - load them from a file or from a database, or whatever - but not list them.

+5


source


I asked a similar question here . The content may be of interest.

The consensus seems to be that the Java enum type is static in design. If you need something that can be changed at runtime, you should ideally use a different data structure.



Less preferred ideas were related to Enum expansion, etc.

+3


source


You can save in a database table.

+1


source







All Articles