Type an enum with a field and a private constructor
Type an enum with a field and a private constructor
Answer
enum Planet { MERCURY(3.303e+23), EARTH(5.976e+24); private final double mass; Planet(double mass) { this.mass = mass; } }
Each constant calls the enum's constructor with the parenthesised argument. The constructor is always private (implicitly if not stated explicitly). ordinal() gives the zero-based position; name() gives the constant name as a String.