To avoid magic numbers, I always use constants in my code. Back in the old days we used to define constant sets in a methodless interface which has now become an antipatter
Enum is best for most of the case, but not everything. Some might be better put like before, that is in a special class with public static constants.
Example where enum is not the best solution is for mathematical constants, like PI. Creating an enum for that will make the code worse.
enum MathConstants {
PI(3.14);
double a;
MathConstants(double a) {
this.a = a;
}
double getValueA() {
return a;
}
}
Usage:
MathConstants.PI.getValueA();
Ugly isn't it? Compare to:
MathConstants.PI;