~/ 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/e5b5d7b5-0da2-43fc-a3dc-0d88be881a61/flashcard utf-8 LF