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;
}
}
The first one, but make it
staticso you don’t make multiple copies of the array.