Reputation: 6440
I have an enum like the following:
public enum Foo {
BAR("v1", "v2", "v3"),
BAZ("x1", "x2", "x3"),
// ...
// version a
private String[] vals;
private Foo(String... vals) {
this.vals = vals;
}
// version b
private String v1, v2, v3;
private Foo(String v1, String v2, String v3) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
}
How do I get a List of "v1", "v2", "v3" back when I know I have a Foo.BAR? Do I have to write my own getters, or is there something built into Enum, much like values() (but for the actual values)?
Upvotes: 0
Views: 187
Reputation: 8640
It depends, if your v1,v2,v3 are some random unrelated values, i think is better to go with version b, and create separate getter for each one
if from other hand they are related, then you could return copy of your array or have single getter like this one
String getValue(int index){
return vals[index-1];
}
Upvotes: 1
Reputation: 11474
You have to write your own getter(s) for custom properties, etc. For your "version A" example, you could do
public String[] getValues() {
return vals;
}
(though you probably would want to return a copy of values, rather than the array itself.)
Upvotes: 1