Ania David
Ania David

Reputation: 1198

java enum pass array as a second parameter ot the construction

Easy

public enum AvailableTestServices {
        UserContext("userContext", ["userURI"]), Level0and1ForUser("level0and1ForUser", ["userURI"]);
    private String serviceName;
    private String[] requiredParameters;

    private AvailableTestServices(String serviceName,
            String[] requriedParameters) {
        this.serviceName = serviceName;
        this.requiredParameters = requriedParameters;
    }

    public String getValue() {
        return serviceName;
    }
    public String[] getRequiredParameters(){
        return this.requiredParameters;
    }
}

I get an exception on the , that is in :

UserContext("userContext", ["userURI"]), Level0and1ForUser("level0and1ForUser", ["userURI"]);

the error is:

Syntax error on token ",", Expression expected after this token AvailableTestServices.java /

enter image description here

Upvotes: 0

Views: 150

Answers (2)

António Ribeiro
António Ribeiro

Reputation: 4202

The proper way to do it should be the following:

UserContext("userContext", new String[]{"userURI"}),
Level0and1ForUser("level0and1ForUser", new String[]{"userURI"});

Upvotes: 2

rdonuk
rdonuk

Reputation: 3956

There is no syntax like this ["Text"] for creating an array. This is the correct way:

public enum AvailableTestServices {
    UserContext("userContext", new String[] { "userURI" }), Level0and1ForUser("level0and1ForUser",
            new String[] { "userURI" });
    private String serviceName;
    private String[] requiredParameters;

    private AvailableTestServices(String serviceName, String[] requriedParameters) {
        this.serviceName = serviceName;
        this.requiredParameters = requriedParameters;
    }

    public String getValue() {
        return serviceName;
    }

    public String[] getRequiredParameters() {
        return this.requiredParameters;
    }
}

Upvotes: 2

Related Questions