1

I'd like to know if it is possible to define an enum with an Array as a constant; see the following code excerpt.

This does not compile with an illegal start of expression error. May anyone help me?.

Many thanks

public enum Currency
{
    *****PENNY(1, {"one", "oneone"}),*
    NICKLE(5, {"five"}),
    DIME(10, {"ten"}),
    QUARTER(25, {"twentifive"});****

    private int valueInteger;
    private String[] valueString;

    private Currency(int valueInteger, String[] valueString) {
        this.valueInteger = valueInteger;
        this.valueString = valueString;
    }

}

2 Answers 2

1

Yes, you'll just need to initialize them. For example,

NICKLE(5, new string[]{"five"})
Sign up to request clarification or add additional context in comments.

Comments

0

For this use case, I would use a vararg approach instead:

public enum Currency {
    PENNY(1, "one", "oneone"),
    NICKLE(5, "five"),
    DIME(10, "ten"),
    QUARTER(25, "twentifive");

    private int valueInteger;
    private String[] valueString;

    private Currency(int valueInteger, String... valueString) {
        this.valueInteger = valueInteger;
        this.valueString = valueString;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.