Possible Duplicate:
Enum values().length vs private field
I want to know how which of these ways to find the size of the enum is better.
This way:
public enum Company {
AUDI(4), BMW(5), CADILLAC(11), FORD(44), JAGUAR(45);
private final int id;
public final int length = Company.values().length;
private Company(int id) {
this.id = id;
}
}
Or this way:
public enum Company {
AUDI(4), BMW(5), CADILLAC(11), FORD(44), JAGUAR(45);
private final int id;
public final int length = 5;
private Company(int id) {
this.id = id;
}
}
given -
enum Widgets {TINY, SMALL, AVERAGE, BIG};
the size of Widgets is:
Widgets.values().length
The first one, but make it static so you don't make multiple copies of the array.
public final static int length = Company.values().length;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With