-1

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;
ner
  • 173
  • 1
  • 1
  • 9
  • 2
    If a method or constructor expects a String, then that is what you will have to pass into it. There is no way to pass a List into it. You either modify the code to accept a List instead of a String, or you make up your own String format that can be converted to a List on demand and add logic for that. (eG use "Value1;Value2;Value3" and then later split that String by semicolon or somthing similar) – OH GOD SPIDERS Jul 27 '22 at 14:13
  • Right. Do you have an example of how to do this conversion? – ner Jul 27 '22 at 14:28
  • 1
    [How to convert a String into an ArrayList?](https://stackoverflow.com/questions/7347856/how-to-convert-a-string-into-an-arraylist) - Contains examples how to convert a String where values are separated by comma into an ArrayList. What symbol is best used for separation depends on your use case. Eg If a list value itself can contain comma then using comma as separator is obviously not a good idea. – OH GOD SPIDERS Jul 27 '22 at 14:37
  • You might want to consider varargs. See e.g. https://stackoverflow.com/questions/372773/are-varargs-allowed-in-a-java-enum-constructor – Ladicek Jul 27 '22 at 14:48
  • I ended up not mentioning it in the post but it's a list of object and not a string. `List pares;` – ner Jul 27 '22 at 14:49
  • 2
    You can make another constructor, `private ParColumnsEnum(List obj) {...}` which will initialize the constant in the appropriate way. – Rogue Jul 27 '22 at 14:53
  • (A) Do you mean a list of objects of a specific class, or a list of `Object` objects? (B) No need to segregate this info in your question with “Update” label; integrate the info into the Question as it originally should have been written. – Basil Bourque Jul 27 '22 at 15:22
  • It is a list of Pares: `List` – ner Jul 27 '22 at 17:31

1 Answers1

1

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. 
    }

}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Since both fields are marked as `final`, they both need to be provided with a value (otherwise it would not compile). They can be assigned either at the lines where they declared on inside the constructors. – Alexander Ivanchenko Aug 07 '22 at 18:01