Reputation: 894
I have enum which looks like this example
public enum Animals {
Cat("Cat", "fluffy animal"),
Dog("Dog", "barking animal");
private String name;
private String description;
}
Animals(String name, String description){
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
From outside class how can I get the name and description Strings ?
Upvotes: 0
Views: 65
Reputation: 45309
You just add getters for your fields:
public enum Animals {
Cat("Cat", "fluffy animal"),
Dog("Dog", "barking animal");
private final String name;
private final String description;
Animals(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return this.name;
}
public String getDescription() {
return this.description;
}
}
And you can access them using the getters:
Animals.Cat.getName();
Animals.Cat.getDescription();
Just a side note: please use all-uppercase identifiers for your enum values. It's also a good idea to make these fields (name
and description
) final.
Upvotes: 6