If this is my enum:
public enum Planet {
MERCURY (3.303e+23),
NEPTUNE (1.024e+26);
private final double mass; // in kilograms
Planet(double mass) {
this.mass = mass;
}
private double mass() {
return mass;
}
}
I can access the values like
Planet.MERCURY.mass();
Is there the chance to define the enum as “double” and let
Planet.MERCURY;
return a double?
Anyway I’m mainly interested in the appearance of the call to ensure code readability.
No. This is not possible with Java representation of enums. Personally I find
Planet.MERCURY.mass()much more readable.The only thing that comes to my mind slightly increasing the readability is making the mass
public finalinstead ofprivate finaland refer toPlanet.MERCURY.massbut I would give it some second thoughts.