How do I pass a List
type value in the constant parameter?
In this case, this parameter is only for the PARS() constant.
public enum ParColumnsEnum { ID_DIST("codDist"), PARS("listValue..."); private final String columnName; ParColumnsEnum(String columnName) { this.columnName = columnName; } public String columnName() { return columnName; } }
Update: I ended up not mentioning it in the post but it’s a list of object and not a string.
List<Pares> pares;
Advertisement
Answer
Enums are objects instantiated from a class, like other objects and classes. So if you have only a single constructor, then every object instantiated must come through that single constructor using its single argument.
As commented, one solution is for you to have more than one constructor.
Here is an example where either of two constructors populate either of two member fields.
public enum ParColumnsEnum { ID_DIST( "codDist" ) , PARS( List.of( LocalDate.now() , 42 , "purple" ) ); // Member fields. // One of these two member fields is always null. Either is populated by either constructor, but never both. private final String columnName; private final List< Object > list; // ð 2 constructors. ParColumnsEnum( String columnName ) { this.columnName = columnName; } ParColumnsEnum( List< Object > list ) { this.list = list; } // Accessors. public String columnName() { return this.columnName; // Might return null. } public List< Object > list() { return this.list ; // Might return null. } }