~/ learn/ java-from-zero/ cards/ Enums: constants, fields, constructors & switch
1 of 4

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/e17f6566-5fd0-4dc0-a6b0-8b23459362fb/flashcard utf-8 LF