Custom fields on java enum not getting serialized

前端 未结 2 1414
后悔当初
后悔当初 2021-01-12 13:41

I have a Java Enum as shown below:

public enum ExecutionMode {
  TYPE_A,
  TYPE_B,
  TYPE_C;

  private ExecutionMode(){} //no args constr- no really require         


        
2条回答
  •  广开言路
    2021-01-12 14:21

    If the values are constant, this is better and you don't need to serialize anything

    public enum ExecutionMode {
      TYPE_A(x,t),
      TYPE_B(y,z),
      TYPE_C(b,s)
    
      private boolean incremental; //has get/set
      private String someStr;      //has get/set
    
      ExecutionMode(boolean incremental,String someStr){
            ///... set things appropriately
      } 
    }
    

    If you're setting these values at runtime, my inclination would be that this shouldn't be an enum in the first place - there should be a separate POJO that perhaps contains the values as well as a reference to an enum value.

提交回复
热议问题