ammf18
ammf18

Reputation: 95

Class Enum java

I have this enum class.

When I call the controller it gives me null

public enum Gender {

    MALE("Male", "M"),
    FEMALE("Female", "F"),
    OTHER("OTHER", "O");

    private String description;
    private String value;


    private Gender(String description, String value) {
        description = description;
        value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return description;
    }
}


List<Gender> listGender = Arrays.asList(Gender.values());
for (Gender o : Gender.values()) {
   System.out.println(o.toString());
}

Answer null

Thanks for what you can help me

Translate with google translate

Upvotes: 0

Views: 164

Answers (1)

Fabienr_75
Fabienr_75

Reputation: 559

You made a mistake inside the enum constructor. Use the keyword this to assign value to object's attributes (an enum is an object).

private Gender(String description, String value) {
    this.description = description;
    this.value = value;
}

Upvotes: 2

Related Questions